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

Dani AI

Generated

Nice question, . For anyone landing here later, you do not have to "link" a ManualResetEvent to a BackgroundWorker. You just need a single, form-level event that the DoWork loop waits on, plus buttons that Set/Reset that same event. Two easy gotchas in the code above: mixing BackgroundWorker1 vs BackgroundWorker2 (your pause/resume checks a different worker than the one running), and creating things locally instead of at class scope (the event, your lists, etc.). Either will lead to Object reference not set if the instance you pause is not the one your loop is waiting on, or if a dependency like eList was never created.

Here is a minimal pattern that is safe and easy to reuse:

' Form-level field
Private ReadOnly pauseEvent As New Threading.ManualResetEventSlim(True)

Private Sub btnStart_Click(...) Handles btnStart.Click
    If Not worker.IsBusy Then
        worker.WorkerSupportsCancellation = True
        worker.RunWorkerAsync()
    End If
End Sub

Private Sub worker_DoWork(...) Handles worker.DoWork
    Using sr As New IO.StreamReader("Sample.txt")
        Do Until sr.EndOfStream OrElse worker.CancellationPending
            pauseEvent.Wait() ' blocks while paused
            Dim line = sr.ReadLine()
            ' process line...
        Loop
        If worker.CancellationPending Then e.Cancel = True
    End Using
End Sub

Private Sub btnPause_Click(...) Handles btnPause.Click
    pauseEvent.Reset()
End Sub

Private Sub btnResume_Click(...) Handles btnResume.Click
    pauseEvent.Set()
End Sub

Private Sub worker_RunWorkerCompleted(...) Handles worker.RunWorkerCompleted
    pauseEvent.Set() ' ensure unpaused for the next run
End Sub

Troubleshooting tips:

  • Use one BackgroundWorker instance consistently in Start/Pause/Resume/DoWork.
  • Initialize every dependency (e.g., eList) before first use.
  • Avoid calling Wait/Reset from the UI thread; only the worker should block.
  • If this is actually ASP.NET, do not use BackgroundWorker for request work; use a background service or queue and return control to the client.

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.