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. Telling a thread to do something.

Telling a thread to do something.

Scheduled Pinned Locked Moved C#
question
4 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.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    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

    L M 2 Replies Last reply
    0
    • C Clive D Pottinger

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      1 Reply Last reply
      0
      • C Clive D Pottinger

        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

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        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 the WarnAll method as soon as ThinksSkyIsFalling==true and then exit WanderAbout - 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. The lock() instruction can come handy in these cases.

        Regards, mav -- Black holes are the places where God divided by 0...

        C 1 Reply Last reply
        0
        • M mav northwind

          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 the WarnAll method as soon as ThinksSkyIsFalling==true and then exit WanderAbout - 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. The lock() instruction can come handy in these cases.

          Regards, mav -- Black holes are the places where God divided by 0...

          C Offline
          C Offline
          Clive D Pottinger
          wrote on last edited by
          #4

          Thanks Mav. I think you hit the root of my question - is a reference, in my main thread, to chickenLittle.ThinkSkyIsFalling valid even though chickenLittle is operating in thread t? 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

          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