Threading, just professionall please...
-
hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot
nobody help you... you have to help you yourself and this is success way.
-
hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot
nobody help you... you have to help you yourself and this is success way.
The only reliable way to kill a thread is to let the thread terminate normally. This means you need a way to notify a thread that it needs to terminate, and provide a way to wait for the thread to terminate if necessary. There's synchronization objects you can use. For example, EventWaitHandle. There's no need to "create my thread public", but you may need to keep contexts to your created threads somewhere, like in a collection in your form class. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot
nobody help you... you have to help you yourself and this is success way.
One way to gracefully terminate a thread is to set a
volatile bool
variable to some value and check this value inside the other thread, like this:public void ButtonClickHandler(Object sender, EventArgs args)
{
requestStopThread = true; // this is a volatile bool variable
}Inside your thread you need to check if this variable will be set to true and then terminate the thread in a correct way:
while(!requestStopThread)
{
// do some stuff, terminate otherwise
}or
// do something
...if(requestStopThread)
// terminate// do something
...regards