Connecting Tech Pros Worldwide Help | Site Map

"Outsourcing" Background worker creation

Newbie
 
Join Date: Dec 2008
Location: Bloominton, CA
Posts: 31
#1: Jun 10 '09
I am trying to code a simple background workers class without needing to use the backgroundworker object on a form. This would allow me to use a background worker in any class without writing much additional code. Is it possible to pass in the name of a sub at runtime instead of hard-coding it? To do this I would replace onDoWork and onProgressChanged below and use (Byval onWorkSub as SomeKindOfObject?, Byval onProgressChangedSub as SomeKindOfObject?) instead.

Expand|Select|Wrap|Line Numbers
  1.         ' Instantiate the worker
  2.         Dim worker As New BackgroundWorker ' Initialize the worker
  3.         worker.WorkerReportsProgress = True
  4.         worker.WorkerSupportsCancellation = True
  5.         AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf onDoWork) ' Create delegate handle
  6.         AddHandler worker.ProgressChanged, New ProgressChangedEventHandler(AddressOf onProgressChanged) ' Create callback handle
  7.  
  8.         '* start the worker
  9.         worker.RunWorkerAsync()
Newbie
 
Join Date: Dec 2008
Location: Bloominton, CA
Posts: 31
#2: Jun 11 '09

re: "Outsourcing" Background worker creation


Is there a way to do this without having as much code? ie, a "proper" way of doing this?

No guarantees if this will do what you want it to. :)

Expand|Select|Wrap|Line Numbers
  1. ''' <summary>Allows tieing events to a worker without additional code</summary>
  2.     Public Class BackgroundWorkerCreation
  3.         Private _worker As BackgroundWorker
  4.  
  5.         ''' <summary>Initialize the worker and tie on events</summary>
  6.         ''' <param name="subDoWork">New DoWorkEventHandler(AddressOf onDoWork)</param>
  7.         ''' <param name="subProgressChanged">New ProgressChangedEventHandler(AddressOf onProgressChanged)</param>
  8.         ''' <param name="subWorkerCompleted">New RunWorkerCompletedEventHandler(AddressOf onWorkerCompleted)</param>
  9.         ''' <param name="SupportCancelation"></param>
  10.         ''' <param name="StartWorkerNow">"False" to start the worker at a later time</param>
  11.         Public Sub CreateWorker(ByVal subDoWork As DoWorkEventHandler, Optional ByVal subProgressChanged As ProgressChangedEventHandler = Nothing, _
  12.                                                                                  Optional ByVal subWorkerCompleted As RunWorkerCompletedEventHandler = Nothing, _
  13.                                                                                  Optional ByVal SupportCancelation As Boolean = False, _
  14.                                                                                  Optional ByVal StartWorkerNow As Boolean = True)
  15.  
  16.             '* To use the background worker, you'll need to type in the arguments similar to what is shown:
  17.  
  18.             'New DoWorkEventHandler(AddressOf subDoWork)
  19.             'New ProgressChangedEventHandler(AddressOf onProgressChanged)
  20.             'New RunWorkerCompletedEventHandler(AddressOf onWorkerCompleted)
  21.  
  22.             ' Instantiate the worker
  23.             Dim worker As New BackgroundWorker
  24.  
  25.             ' Tie in the DoWork event
  26.             AddHandler worker.DoWork, subDoWork ' Create delegate handle
  27.  
  28.             ' Tie in the ReportsProgress event
  29.             If subProgressChanged Is Nothing Then
  30.                 worker.WorkerReportsProgress = False
  31.             Else
  32.                 worker.WorkerReportsProgress = True
  33.                 AddHandler worker.ProgressChanged, subProgressChanged ' Create callback handle
  34.             End If
  35.  
  36.             ' Tie in the RunWorkerCompleted event
  37.             If subProgressChanged IsNot Nothing Then AddHandler worker.RunWorkerCompleted, subWorkerCompleted ' Create completion handle
  38.  
  39.             ' Allow Cancelation
  40.             worker.WorkerSupportsCancellation = SupportCancelation
  41.  
  42.             '* Start the worker
  43.             If StartWorkerNow = True Then StartWorker()
  44.         End Sub
  45.  
  46.         ''' <summary>Start or Re-start the background worker. If the worker is still running, this sub will do nothing.</summary>
  47.         Public Sub StartWorker()
  48.             If _worker.IsBusy = False Then _worker.RunWorkerAsync()
  49.         End Sub
  50.  
  51.         ''' <summary>Notify the worker that it should cancel. Will only work if the worker supports cancellation.</summary>
  52.         Public Sub CancelWorker()
  53.             If _worker.WorkerSupportsCancellation = True And _worker.IsBusy Then _worker.CancelAsync()
  54.         End Sub
  55.  
  56.         ''' <summary>Releases all resources used by the System.ComponentModel.Component</summary>
  57.         Public Sub Dispose()
  58.             _worker.Dispose()
  59.         End Sub
  60.     End Class
Reply

Tags
addhandler, asynchronous, backgroundworker, dynamic, threading


Similar Visual Basic .NET bytes