[RESOLVED] BackgroundWorker Pause?

Status
Not open for further replies.

PutterPlace

Active member
Joined
Feb 18, 2008
Messages
37
Programming Experience
1-3
I am working on a small project to get download links from my web site. I require word verification in image form (AKA captcha) in order to download files from my site. Right now, this project which contains two Background Workers. Since these background workers work in a chain (sometimes causing problems when one of the threads doesn't close in a timely manner), I would like to combine them into 1 Background Worker.

This is the basic breakdown of my current code:

VB.NET:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    ' INFORMATION PROCESSING
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    WebBrowser1.Url = New Uri("http://www.MyWebSite.com/SomeDirectory/MyImage.jpg")
End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If (InStr(WebBrowser1.Url.ToString, "MyImage.jpg")) Then
        ' THE TEXTBOX IS ENABLED, WAIT FOR USER TO ENTER CHARACTERS FROM IMAGE
        lblStatus.Text = "Waiting for input...."
        txtWordVerification.Enabled = True
    End If
End Sub

Private Sub txtWordVerification_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWordVerification.TextChanged
    ' PLACE WORD VERIFICATION TEXT INTO SETTINGS
    My.Settings.WordVerification = txtWordVerification.Text
End Sub

Private Sub txtWordVerification_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWordVerification.KeyDown
    ' THE ENTER KEY HAS BEEN PRESSED, START 2ND BACKGROUND WORKER
    If e.KeyCode = Keys.Enter Then
        txtWordVerification.Enabled = False
        lblStatus.Text = "Getting Download Link"
        BackgroundWorker2.RunWorkerAsync()
    End If
End Sub

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    ' MORE INFORMATION PROCESSING
End Sub

I know how to make the thread "sleep", but I only know how to do this for a period of time, not just until an action is performed. Is there any way that I could just pause BackgroundWorker1 until the enter key is pressed from GUI thread? This will eliminate the need to start a second Background Worker.
 
Last edited:
Nevermind. I found a way to do it. I moved all information from the BackgroundWorker2 DoWork event and put it into BackgroundWorker1. Then when I needed the user to input the characters from the image, I set a setting (My.Settings.ContinueThread) to False. Right after that, I started a loop to check that setting every 3 seconds. When the KeyDown event is fired (and the ENTER key was pressed), then I changed the ContinueThread setting to True. The thread continued automatically once it found that ContinueThread was changed to True.

:)
 
You can use a wait handle to synchronize the threads, like AutoResetEvent or ManualResetEvent classes.
 
Status
Not open for further replies.
Back
Top