Reputation: 737
I want to create two buttons that would pause and resume my backgroundworker but i don't know where do i start with my code, i have just started learning VB.net and wanted to learn about pause and resume function of backgroundworker.
Here is my existing BackgroundWorker code that i wish to pause and resume
Public resetevent As New ManualResetEvent(False)
Dim boo As Nullable(Of Boolean) = True
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
If BackgroundWorker2.IsBusy <> True Then
BackgroundWorker2.RunWorkerAsync()
resetevent.Set()
End If
Catch ex As Exception
End Try
End Sub
Private Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim worker2 As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Try
Dim Stream As New System.IO.FileStream("Sample.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(Stream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
resetevent.WaitOne()
Thread.Sleep(500)
eList.Add(sReader.ReadLine)
Delay(1)
Loop
eArray = eList.ToArray
Thread.Sleep(1000)
Stream.Close()
sReader.close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Try
Catch ex As Exception
End Try
End Sub
Private Sub BackgroundWorker2_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
Try
resetevent.Reset()
Catch ex As Exception
End Try
End Sub
Private Sub pause_Click(sender As Object, e As EventArgs) Handles pause.Click
pause.Enabled = False
resme.Enabled = True
If BackgroundWorker1.IsBusy Then
boo = False
resetevent.Reset()
End If
End Sub
Private Sub resme_Click(sender As Object, e As EventArgs) Handles resme.Click
resme.Enabled = False
pause.Enabled = True
If BackgroundWorker1.IsBusy Then
boo = True
resetevent.Set()
End If
This code is now working thanks for the tips
Upvotes: 3
Views: 6154
Reputation: 3045
Please take a look at
Pause/Resume loop in Background worker
and
How to pause and resume a BackgroundWorker?
also
How to implement Pause & Resume functionality with BackgroundWorker c#
Use a converter for code that you need converted.. those should all have an answer for you.
Upvotes: 3