Only allow one call to a function at a time
-
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
-
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
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[^]
-
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[^]
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
-
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
Yes its ok that you are not using threads. Mutex works on single threaded applications as well.
-
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
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