Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Only allow one call to a function at a time

Only allow one call to a function at a time

Scheduled Pinned Locked Moved C#
helpquestiondiscussion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dwayner79
    wrote on last edited by
    #1

    I have two controls each of which have loops (we will call loop 1 and loop 2) that call a central function that sends and gets data via serial (we will call Communication function). Only one loop is to run at a time. The problem I am having is when the user requests loop 2 while loop 1 just started. Loop 2 alerts loop 1 to stop, but if loop 1 just started, it will still be calling the communication function until it finishes. I hope this is makeing sense... Meanwhile, loop 2 has started calling communication function as well and I am getting overlap. I cannot simple set a bit and exit the loop at the end. (It is a big program so you will just have to trust me.) Is there a way to cache the values needed to be sent to the communications function? Is there a clean way to exit a loop without it finishing? Should I be focussing on trying to make the two (in reality 10) different loops NEVER call until all other loops are inactive, or should I be trying to make the communication function able to handle multiple calls? Thanks in advance for your thoughts. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

    P J 2 Replies Last reply
    0
    • D Dwayner79

      I have two controls each of which have loops (we will call loop 1 and loop 2) that call a central function that sends and gets data via serial (we will call Communication function). Only one loop is to run at a time. The problem I am having is when the user requests loop 2 while loop 1 just started. Loop 2 alerts loop 1 to stop, but if loop 1 just started, it will still be calling the communication function until it finishes. I hope this is makeing sense... Meanwhile, loop 2 has started calling communication function as well and I am getting overlap. I cannot simple set a bit and exit the loop at the end. (It is a big program so you will just have to trust me.) Is there a way to cache the values needed to be sent to the communications function? Is there a clean way to exit a loop without it finishing? Should I be focussing on trying to make the two (in reality 10) different loops NEVER call until all other loops are inactive, or should I be trying to make the communication function able to handle multiple calls? Thanks in advance for your thoughts. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

      P Offline
      P Offline
      Peter Vertes
      wrote on last edited by
      #2

      You could use a Mutex. You set Mutex inside your Communication function in the beginning and release it at the end of it. Your loops will only be able to run your Communication function if they can aquire the Mutex. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmutexclasstopic.asp[^]

      D 1 Reply Last reply
      0
      • P Peter Vertes

        You could use a Mutex. You set Mutex inside your Communication function in the beginning and release it at the end of it. Your loops will only be able to run your Communication function if they can aquire the Mutex. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmutexclasstopic.asp[^]

        D Offline
        D Offline
        Dwayner79
        wrote on last edited by
        #3

        beautiful... I think that will work. Is it ok that I am not using threads. The call is subscribed to an event so I am not actually creating a new thread. I will try it and get back. Thanks, Dwayne ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

        P 1 Reply Last reply
        0
        • D Dwayner79

          beautiful... I think that will work. Is it ok that I am not using threads. The call is subscribed to an event so I am not actually creating a new thread. I will try it and get back. Thanks, Dwayne ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

          P Offline
          P Offline
          Peter Vertes
          wrote on last edited by
          #4

          Yes its ok that you are not using threads. Mutex works on single threaded applications as well.

          1 Reply Last reply
          0
          • D Dwayner79

            I have two controls each of which have loops (we will call loop 1 and loop 2) that call a central function that sends and gets data via serial (we will call Communication function). Only one loop is to run at a time. The problem I am having is when the user requests loop 2 while loop 1 just started. Loop 2 alerts loop 1 to stop, but if loop 1 just started, it will still be calling the communication function until it finishes. I hope this is makeing sense... Meanwhile, loop 2 has started calling communication function as well and I am getting overlap. I cannot simple set a bit and exit the loop at the end. (It is a big program so you will just have to trust me.) Is there a way to cache the values needed to be sent to the communications function? Is there a clean way to exit a loop without it finishing? Should I be focussing on trying to make the two (in reality 10) different loops NEVER call until all other loops are inactive, or should I be trying to make the communication function able to handle multiple calls? Thanks in advance for your thoughts. ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

            J Offline
            J Offline
            jan larsen
            wrote on last edited by
            #5

            As Peter says, you can use a Mutex. But if you don't need to synchronize between processes, you're better of using lock:

            public sealed class MyClass
            {
            internal void Foo()
            {
            lock(this)
            {
            // Access to this area is synchronized.
            }
            }

            internal void Bar()
            {
            lock(this)
            {
            // Access to this area is synchronized.
            }
            }
            }

            "God doesn't play dice" - Albert Einstein "God not only plays dice, He sometimes throws the dices where they cannot be seen" - Niels Bohr

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups