I want to create two buttons that would pause and resume my backgroundworker but it is giving me an exception error of "Object reference not set to an instance of an object". It seems like the resetevent is not linked with my BackgroundWorker and i don't know how to link them

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

i have already updated my code and solved it. this can serve as a reference to others. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.