safely end a thread
-
Hi, I'm trying to end 3 threads in a save way. I can get 2 threads to stop, but the last one doesn't seem to abort. My try:
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool stopRequested = false;this.stopRequested = true;
// Use the Join method to block the current thread
// until the object's thread terminates.
this.thdPageContent1.Join();this.thdPageContent2.Join();
this.thdFileComparer.Join();
After the thdFileComparer thread, i got a messagebox that 'tells' me the threads are stopped: MessageBox.Show("Threads stopped successfully"); Any idea what I might be doing wrong? Thanks in advance!
-
Hi, I'm trying to end 3 threads in a save way. I can get 2 threads to stop, but the last one doesn't seem to abort. My try:
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool stopRequested = false;this.stopRequested = true;
// Use the Join method to block the current thread
// until the object's thread terminates.
this.thdPageContent1.Join();this.thdPageContent2.Join();
this.thdFileComparer.Join();
After the thdFileComparer thread, i got a messagebox that 'tells' me the threads are stopped: MessageBox.Show("Threads stopped successfully"); Any idea what I might be doing wrong? Thanks in advance!
The join method only waits for a thread to end... it does not abort the thread. So, if your thread is in an infinite loop (or infinite wait), your join will be waiting infinitelly. I must say I need to know the thread code to know what's wrong... but, if you are using some event (ManualResetEvent, for example) in the 3rd thread, you must set it. So, the thread can execute (and check the stopRequested variable).
-
Hi, I'm trying to end 3 threads in a save way. I can get 2 threads to stop, but the last one doesn't seem to abort. My try:
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool stopRequested = false;this.stopRequested = true;
// Use the Join method to block the current thread
// until the object's thread terminates.
this.thdPageContent1.Join();this.thdPageContent2.Join();
this.thdFileComparer.Join();
After the thdFileComparer thread, i got a messagebox that 'tells' me the threads are stopped: MessageBox.Show("Threads stopped successfully"); Any idea what I might be doing wrong? Thanks in advance!
Yustme wrote:
the last one doesn't seem to abort
Aborting a thread is a bad idea as it leaves things in an unknown state. But then maybe you're not really calling Thread.Abort() at all?
Yustme wrote:
this.stopRequested = true;
This doesn't do anything, unless your thread is periodically checking that variable. But then you did not show such code, making me doubt you understand how your code is working. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
-
The join method only waits for a thread to end... it does not abort the thread. So, if your thread is in an infinite loop (or infinite wait), your join will be waiting infinitelly. I must say I need to know the thread code to know what's wrong... but, if you are using some event (ManualResetEvent, for example) in the 3rd thread, you must set it. So, the thread can execute (and check the stopRequested variable).
Hi, What do I need to do to end the thread after the Join? The 3rd thread has a while loop and in side that while loop i got a nested for statement. One for statement in another one. I check for the volatile variable in my while loop. And once more inside the deepest for statement. That should be enough right?
-
Hi, What do I need to do to end the thread after the Join? The 3rd thread has a while loop and in side that while loop i got a nested for statement. One for statement in another one. I check for the volatile variable in my while loop. And once more inside the deepest for statement. That should be enough right?
If you check the variable, it should end. Are you sure there is nothing more happening? Can't you put a break point in the last join and, instead of executing it, as all threads are stopped, check what's happening in the other thread? Maybe put some breakpoints at that moment and then allow the Join to go... you must see exactly what the other thread is doing.
-
If you check the variable, it should end. Are you sure there is nothing more happening? Can't you put a break point in the last join and, instead of executing it, as all threads are stopped, check what's happening in the other thread? Maybe put some breakpoints at that moment and then allow the Join to go... you must see exactly what the other thread is doing.
Yes I'm sure. The variable gets checked alright. But it doesn't seem to stop it. Here is that thread i'm talking about with the code:
private void FileComparerThread()
{while (this.stopRequested != true) { for (int i = 0; i < Directory.GetFiles(this.dest).Length; i++) { string\[\] filePaths = Directory.GetFiles(this.dest, "\*.txt"); for (int j = 0; j < filePaths.Length; j++) { // check if btnCancel has been pressed if so, end this thread. if (this.stopRequested != true) { // check if file names are not the same: if (filePaths\[i\].ToString() != filePaths\[j\].ToString()) { bool comparedFiles = this.FileCompare(filePaths\[i\].ToString(), filePaths\[j\].ToString()); // move 1 file if they are both the same to another directory if (comparedFiles) Directory.Move(filePaths\[j\], Path.Combine(this.dest2, Path.GetFileName(filePaths\[j\]))); } } } if(filePaths.Length > 3) File.Move(filePaths\[i\], Path.Combine(this.dest, Path.GetFileName(filePaths\[i\]))); Thread.Sleep(5000); } } }
And then I got a this.FileCompare() function that checks 2 files for equality thats it. I've done some big time debugging. Its just looping those for statements. Even though the stopRequested = true. It's not comming out that for statement.
-
Yustme wrote:
the last one doesn't seem to abort
Aborting a thread is a bad idea as it leaves things in an unknown state. But then maybe you're not really calling Thread.Abort() at all?
Yustme wrote:
this.stopRequested = true;
This doesn't do anything, unless your thread is periodically checking that variable. But then you did not show such code, making me doubt you understand how your code is working. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
-
Yes I'm sure. The variable gets checked alright. But it doesn't seem to stop it. Here is that thread i'm talking about with the code:
private void FileComparerThread()
{while (this.stopRequested != true) { for (int i = 0; i < Directory.GetFiles(this.dest).Length; i++) { string\[\] filePaths = Directory.GetFiles(this.dest, "\*.txt"); for (int j = 0; j < filePaths.Length; j++) { // check if btnCancel has been pressed if so, end this thread. if (this.stopRequested != true) { // check if file names are not the same: if (filePaths\[i\].ToString() != filePaths\[j\].ToString()) { bool comparedFiles = this.FileCompare(filePaths\[i\].ToString(), filePaths\[j\].ToString()); // move 1 file if they are both the same to another directory if (comparedFiles) Directory.Move(filePaths\[j\], Path.Combine(this.dest2, Path.GetFileName(filePaths\[j\]))); } } } if(filePaths.Length > 3) File.Move(filePaths\[i\], Path.Combine(this.dest, Path.GetFileName(filePaths\[i\]))); Thread.Sleep(5000); } } }
And then I got a this.FileCompare() function that checks 2 files for equality thats it. I've done some big time debugging. Its just looping those for statements. Even though the stopRequested = true. It's not comming out that for statement.
I think I know what's the problem. You didn't return in the internal if. So, stopRequested is true. Your method does not compare the files, but continue in it's loop, waits for 5 seconds and so on. So, I think instead of:
if (this.stopRequested != true)
{
... some code...
}You should use:
if (stopRequested)
return; -
I think I know what's the problem. You didn't return in the internal if. So, stopRequested is true. Your method does not compare the files, but continue in it's loop, waits for 5 seconds and so on. So, I think instead of:
if (this.stopRequested != true)
{
... some code...
}You should use:
if (stopRequested)
return;