Telling a thread to do something.
-
This whole multi-threading structure keeps shifting in and out of focus like a Dali painting after 5 pints of Guinness... Okay, I have the following going in my OldMcDonald process:
PanickyChicken chickenLittle = new PanickyChicken(); Thread t = new Thread(new ThreadStart(chickenLittle.WanderAbout)); t.Start(); while ( !t.IsAlive ); this.WaitForAcornToDrop(); ...
Now, once the WaitForAcornToDrop method has executed, I want to get my chickenLittle thread (t) to execute its WarnAll method [e.g. chickenLittle.WarnAll("The sky is falling");] How do I tell an executing thread to perform a task?Clive Pottinger Victoria, BC
-
This whole multi-threading structure keeps shifting in and out of focus like a Dali painting after 5 pints of Guinness... Okay, I have the following going in my OldMcDonald process:
PanickyChicken chickenLittle = new PanickyChicken(); Thread t = new Thread(new ThreadStart(chickenLittle.WanderAbout)); t.Start(); while ( !t.IsAlive ); this.WaitForAcornToDrop(); ...
Now, once the WaitForAcornToDrop method has executed, I want to get my chickenLittle thread (t) to execute its WarnAll method [e.g. chickenLittle.WarnAll("The sky is falling");] How do I tell an executing thread to perform a task?Clive Pottinger Victoria, BC
Hi, a thread executes whatever code is passed to it at creation time, in your example it will run the WanderAbout() method until that method is done. So whatever you wnat the thread to do must be inside that method, you can't make it suddenly do something completely different. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
This whole multi-threading structure keeps shifting in and out of focus like a Dali painting after 5 pints of Guinness... Okay, I have the following going in my OldMcDonald process:
PanickyChicken chickenLittle = new PanickyChicken(); Thread t = new Thread(new ThreadStart(chickenLittle.WanderAbout)); t.Start(); while ( !t.IsAlive ); this.WaitForAcornToDrop(); ...
Now, once the WaitForAcornToDrop method has executed, I want to get my chickenLittle thread (t) to execute its WarnAll method [e.g. chickenLittle.WarnAll("The sky is falling");] How do I tell an executing thread to perform a task?Clive Pottinger Victoria, BC
In addition to what Luc said, you can influence what's happening inside your thread's procedure for example by setting properties of your object which in turn are used inside the thread procedure. For example, your PanickyChicken class could have a property
public bool ThinksSkyIsFalling = false {get; set;}
(don't know if that's the correct C# 3.5 syntax, but I think you get the idea). Now your chicken is wandering about until your program tells it that the sky is falling:
chickenLittle.ThinksSkyIsFalling = true;
Now inside your
WanderAbout
method you can execute theWarnAll
method as soon asThinksSkyIsFalling==true
and then exitWanderAbout
- chickenLittle's job is done. But then you'll have to think about synchronization issues. In the example above I think there's not much that can go wrong without explicit synchronization, but when you're performing operations that can be interrupted by a thread switch, you'll definitely have to take care of these cases. Thelock()
instruction can come handy in these cases.Regards, mav -- Black holes are the places where God divided by 0...
-
In addition to what Luc said, you can influence what's happening inside your thread's procedure for example by setting properties of your object which in turn are used inside the thread procedure. For example, your PanickyChicken class could have a property
public bool ThinksSkyIsFalling = false {get; set;}
(don't know if that's the correct C# 3.5 syntax, but I think you get the idea). Now your chicken is wandering about until your program tells it that the sky is falling:
chickenLittle.ThinksSkyIsFalling = true;
Now inside your
WanderAbout
method you can execute theWarnAll
method as soon asThinksSkyIsFalling==true
and then exitWanderAbout
- chickenLittle's job is done. But then you'll have to think about synchronization issues. In the example above I think there's not much that can go wrong without explicit synchronization, but when you're performing operations that can be interrupted by a thread switch, you'll definitely have to take care of these cases. Thelock()
instruction can come handy in these cases.Regards, mav -- Black holes are the places where God divided by 0...
Thanks Mav. I think you hit the root of my question - is a reference, in my main thread, to
chickenLittle.ThinkSkyIsFalling
valid even thoughchickenLittle
is operating in threadt
? It appears it is - great. That gives me the groundwork I needed to get information to my executing thread. Thanks again.Clive Pottinger Victoria, BC