Threading

  • Thread starter Thread starter Anthony Nystrom
  • Start date Start date
A

Anthony Nystrom

What is the correct way to stop a thread? abort? sleep? Will it start up
again... Just curious... If the thread is enabling a form, if the form is
disposed is the thread as well?

Thanks,

Anthony Nystrom
 
Anthony Nystrom said:
What is the correct way to stop a thread? abort? sleep? Will it start
up again... Just curious...

"Cleanest" way is to set a flag checked by the thread exitting itself if
set. Exitting means to exit procedures in reverse order til the thread main
procedure and it's end is reached.
If the thread is enabling a form, if the
form is disposed is the thread as well?

No, but if the thread ends, the form is killed.
 
Hello Anthony,

Thanks for posting in the community.

Based on my understanding, the question is: How to create, start, pause,
resume, and abort threads? If a form is created in a thread, when the form
exits, does the thread exit?

In MSDN, there is a great part on threading programming. Please refer to
the following link:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconthreading.asp?fram
e=true

It includes:

1) Managed Threading Basics
Provides an overview of managed threading and discusses when to use
multiple threads.

2) Using Threads and Threading
Explains how to create, start, pause, resume, and abort threads.

3) Managed Threading Best Practices
Discusses levels of synchronization, how to avoid deadlocks and race
conditions, single-processor and multiprocessor computers, and other
threading issues.

4) Threading Objects and Features
Describes the managed classes you can use to synchronize the activities of
threads and the data of objects accessed on different threads, and provides
an overview of thread pool threads.

I believe it can help you much. For the second question, it depends on how
you lauch the form in that thread. For an example, if you create a WinForm
application from application wizard in VS.NET, the main thread of the
program contains a Form. After the form is disposed by user interactin, the
function of thread execution will quit also. Under this situation, the
thread quits when the form is disposed. However, if the function of thread
has some other tasks to do after the form is disposed, it won't end.

Does that answer your question? If there is anything unclear, please feel
free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Armin,

I'd suggest using a synchronization object such as an event instead of a
boolean flag. This enables to avoid unnecessary flag polling and therefore
to prevent CPU time waste.
 
Dmitriy Lapshin said:
I'd suggest using a synchronization object such as an event instead
of a boolean flag. This enables to avoid unnecessary flag polling and
therefore to prevent CPU time waste.

How can the thread know when to exit?

do
'work

loop until cancelflag

How can this replaced by a synchronization object?
 
It would be something like:

do
'work

loop until event.Wait(0, False)

But, in your scenario, the boolean flag should work fine as well.
Synchronization objects are useful when you wait for some other
synchronization objects in your thread and you wish to have an ability to
cancel the waiting. The, you can use a kind of WaitMultiple method and
analyze its outcome to determine which of the synchronization objects has
been signaled.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
Dmitriy Lapshin said:
It would be something like:

do
'work

loop until event.Wait(0, False)

But, in your scenario, the boolean flag should work fine as well.
Synchronization objects are useful when you wait for some other
synchronization objects in your thread and you wish to have an
ability to cancel the waiting. The, you can use a kind of
WaitMultiple method and analyze its outcome to determine which of the
synchronization objects has been signaled.

Thanks! I'll have a look at it - as soon as I need it. :-)
 
Hello Anthony,

How are things going? I would appreciate it if you could post here to let
me know the status of the issue. If you have any questions or concerns,
please don't hesitate to let me know. I look forward to hearing from you,
and I am happy to be of assistance.

Thanks for participating the community.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top