dcsimg
Results 1 to 4 of 4

Thread: Does a thread stop automatically when the procedure is finished?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2018
    Posts
    3

    Does a thread stop automatically when the procedure is finished?

    If I use a thread that I start with myThred.Start. Then when the procedure that is assigned to that thread is finished, does it close the thread? or do I still have to call Thread.Stop?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,395

    Re: Does a thread stop automatically when the procedure is finished?

    Under the remarks section of the Threading.Thread class it contains this bit:
    It is not necessary to retain a reference to a Thread object once you have started the thread. The thread continues to execute until the thread procedure is complete.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,539

    Re: Does a thread stop automatically when the procedure is finished?

    If you don't loop, i.e. you exit the procedure that is your thread, then the thread is finished.
    You'll have create a new instance of the thread and start that one to run again.

    You may want to consider using a Background worker if you don't plan on maintaining a persistent running background task. Or if you're using something later than VS2010, I believe there is another mechanism for running short background tasks easily.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,241

    Re: Does a thread stop automatically when the procedure is finished?

    Yes, there is, and it was around starting with FW 4.0 in VS2010. Look into the Task. The Task is supposed to be a bit lighter in impact than a thread, though I'm not quite sure what that means. I doubt there's much practical impact of that. After the initial debut in FW 4.0, new features were added that enhanced the utility of the Task. In the initial implementation, you can fire off a series of Tasks, wait for them to complete, or chain subsequent actions to take after the initial Task completes. The variety of things that can be done was what was enhanced in FW 4.5 and onwards.

    A Background Worker is really convenient if you want to be able to periodically post updates back to the UI thread, since it has a built in mechanism to do so. With a thread, you have to make your own, which isn't hard, but it's given to you automatically with the BGW. The Task is a bit lighter than a thread.

    Personally, I prefer to use a Thread if I have something that will run for a long time in the background, such as something waiting for communications from other systems. I use a BGW if I have some process that I start from a form, and which needs to be able to update some UI element, or otherwise notify the UI of progress, and I use a Task for everything else. For example, if I have a whole bunch of calls to web services, Tasks would be appropriate because I could fire them all off and WaitAll on the outcome.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width