Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
hello friend i have   Qu. that how to stop running thread. i created window form appln.
in that appln on button click i create one thread .so thread procedure contain function call from other dll.my requiement is when i click on cancel button it will show message to user that "do u want close or not" so my que. is on click of cancel button how to stop my thread as well as my function execution.
Posted 20-Jan-12 2:41am
Comments
chandanadhikari at 21-Jan-12 1:07am
   
well not very advisable but see if DoEvents can help you out
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Pausing thread has been recognized as dangerous and is not available in .NET.
 
Here is what you need to use instead of Pause/Resume: one of the classes: System.Threading.EventWaitHandle, System.Threading.AutoResetEvent or System.Threading.ManualResetEvent; please see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[^].
 
The first class can be used instead the other two; they are only different in the way how the event handle is reset: automatically or only explicitly.
 
The idea is this: the event has two states, signaled or non-signaled. One can signal the event by the call to Set and un-signal calling Reset or it will be reset automatically by a waiting thread. Now, you pause the thread by calling EventWaitHandle.WaitOne by the thread to be paused. If the event is non-signaled, the thread is put in the wait state: it is switched out by the OS and never scheduled back to execution until it is awaken. It will be awaken if some other thread calls Set on the same instance of event wait handle; it also will be awaken by a timeout (optional wait parameter) or Thread.Abort.
 
When a thread is awaken and the event handle uses auto-reset, it will be reset back to non-signaled state immediately on the wake-up of the thread. This transition is atomic and cannot be reliably simulated by other methods (such as Boolean condition). This auto-reset mechanism is designed to pass one and only one waiting thread at a time and make other threads waiting on the same event handle instance in the wait state.
 
—SA
  Permalink  
v3
Comments
Prashant Srivastava LKO at 21-Jan-12 5:11am
   
Satisfactory and Nice answer sir my 5
SAKryukov at 21-Jan-12 12:31pm
   
Thank you, Prashant.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100