Connecting Tech Pros Worldwide Forums | Help | Site Map

Wake up Thread from Sleep in BackgroundWorker

Newbie
 
Join Date: May 2007
Posts: 5
#1: May 23 '07
I am a new user of Visual Basic. I am currently using VB 2005.

I am running a time-consuming task on the built-in BackgroundWorker() control of VB. I am using nested loops and sometimes I need to issue Thread.Sleep() while waiting for a specific keyword from my TextBox display window.

How do I wake up the BackgroundWorker() thread and resume my code when I detect the keyword that I have been waiting for? How do I call an Interrupt from another thread to wake the sleeping thread?

How do I incorporate the following code to BackgroundWorker()?

Try
Thread.Sleep()
Catch ex As ThreadInterruptedException
'wake up from sleep
End Try

Thank you very much in advance.


-- ercastro

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Jun 2 '07

re: Wake up Thread from Sleep in BackgroundWorker


Quote:

Originally Posted by ercastro

I am a new user of Visual Basic. I am currently using VB 2005.

I am running a time-consuming task on the built-in BackgroundWorker() control of VB. I am using nested loops and sometimes I need to issue Thread.Sleep() while waiting for a specific keyword from my TextBox display window.

How do I wake up the BackgroundWorker() thread and resume my code when I detect the keyword that I have been waiting for? How do I call an Interrupt from another thread to wake the sleeping thread?

How do I incorporate the following code to BackgroundWorker()?

Try
Thread.Sleep()
Catch ex As ThreadInterruptedException
'wake up from sleep
End Try

Thank you very much in advance.


-- ercastro

Give this one a go:

http://msdn2.microsoft.com/en-us/library/8xs8549b.aspx
Newbie
 
Join Date: May 2007
Posts: 5
#3: Jun 6 '07

re: Wake up Thread from Sleep in BackgroundWorker


Thank you for responding. I have actually used BackgroundWorker() already. My problem is how to wake up the BackgroundWorker() thread from Thread.Sleep using an Interrupt (ThreadInterruptException).


-- ercastro
Newbie
 
Join Date: May 2007
Posts: 5
#4: Jun 8 '07

re: Wake up Thread from Sleep in BackgroundWorker


Quote:

Originally Posted by ercastro

Thank you for responding. I have actually used BackgroundWorker() already. My problem is how to wake up the BackgroundWorker() thread from Thread.Sleep using an Interrupt (ThreadInterruptException).


-- ercastro

Let me clarify what I need. I am using BackgroundWorker() to run a time-consuming task. I put the BackgroundWorker thread to sleep indefinitely by Thread.Sleep(Timeout.Infinite). I require 2 ways to wake the sleeping thread, whichever happens FIRST:
(1) Issue a Thread.Interrupt in the main thread when I detect a particular string in my text box (data from serial port), and catch this interrupt in BackgroundWorker as ThreadInterruptedException;
(2) As a backup, put a maximum time delay in case the desired string does not come up so that the BackgroundWorker thread will not sleep infinitely

I have already implemented step (1) after some research and experimentations. Now, my problem lies with step (2). I tried using the Timer control to fire up after the maximum time delay and issue a Thread.Interrupt, but the this never happens and BackgroundWorker sleeps forever. The Timer is enabled. I issue Timer.Start before issuing Thread.Sleep(Timeout.Infinite) at BackgroundWorker.

Thank you very much in advance.


-- ercastro
Reply