Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a background thread and in DoWork I have to some stuff, in two stages. So I use Thread.Sleep(n) in the middle of and everything works fine.

However, now I have a requirement to suspend the work doing in DoWork. Even I can do that I cannot terminate the Thread.Sleep() action. Say the current thread (background thread in my case) set to sleep for 10 seconds, DoWrok complete after 10 seconds or so after I send the cancellation.

How can I resolve this. Comments really appreciate.
Posted
Comments
[no name] 16-Jan-14 0:36am    
I was about to answer but won't after your reply to Dave who gave you the correct answer based on your information. Who gave you the idea that putting a thread to sleep and then terminating it was good approach? You are on the first rung of the thread ladder with many more to climb. Listen and learn.
CodingLover 16-Jan-14 0:55am    
Thanks!

How do you resolve that?? Don't use Thread.Sleep! It puts the thread to sleep and is won't do anything at all, including terminate!, until the Sleep returns.

Since you don't explain what your worker thread is doing and why you put Thread.Sleep in there, it's pretty much impossible to give you any kind of accurate answer as to what you should be doing instead.
 
Share this answer
 
Comments
CodingLover 15-Jan-14 23:57pm    
Sorry if I'm not explain well enough.

What I'm doing is write a line of text into a file and wait the specified time (using Thread.Sleep), and then write another line to the same file. What I want to do is, if I cancel the background thread while in the Thread.Sleep I want to immediately terminate the all. Now what happen is after the Thread.Sleep elapse only the stuff on cancellation happens. Is that clear to you now :)
Dave Kreskowiak 16-Jan-14 0:09am    
Thanks for the 1-vote ass hat. Completely unjustified.

CodingLover 16-Jan-14 0:55am    
Seems you've misunderstood. I didn't vote on any answer/comment on this question.
Maciej Los 16-Jan-14 2:15am    
Good answer, +5!
 
Share this answer
 
Comments
CodingLover 16-Jan-14 6:59am    
Thanks a lot. I'll go through it and see.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900