Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Implementing the Worker Delegate

No matter which version of multithreading you are using (Thread, thread pools, or BackgroundWorker), the work that the thread does is represented by a delegate, or an event handler if you prefer. The primary connect point for the BackgroundWorker component is the DoWork event. If we attach a DoWorkEventHandler delegate to the DoWork event, we need to have some work for the thread component to do. Listing 2 demonstrates how to define a work-event handler and attach it to the DoWork event.

Listing 2 Defining a worker delegate for the BackgroundWorker component.

Imports System.ComponentModel

Module Startup

 Sub Main()
  Dim worker As BackgroundWorker = New BackgroundWorker()
  AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf OnWork)

 End Sub

 Public Sub OnWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

 End Sub

End Module

Wiring an event handler for the BackgroundWorker component is no different then wiring any other event handler. Simply figure out the signature of the delegate and assign the AddressOf to the event property.

Next, we need to define some work and start the thread.

  • + Share This
  • 🔖 Save To Your Account