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. -
' Instantiate the worker
-
Dim worker As New BackgroundWorker ' Initialize the worker
-
worker.WorkerReportsProgress = True
-
worker.WorkerSupportsCancellation = True
-
AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf onDoWork) ' Create delegate handle
-
AddHandler worker.ProgressChanged, New ProgressChangedEventHandler(AddressOf onProgressChanged) ' Create callback handle
-
-
'* start the worker
-
worker.RunWorkerAsync()
1 2006
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. :) - ''' <summary>Allows tieing events to a worker without additional code</summary>
-
Public Class BackgroundWorkerCreation
-
Private _worker As BackgroundWorker
-
-
''' <summary>Initialize the worker and tie on events</summary>
-
''' <param name="subDoWork">New DoWorkEventHandler(AddressOf onDoWork)</param>
-
''' <param name="subProgressChanged">New ProgressChangedEventHandler(AddressOf onProgressChanged)</param>
-
''' <param name="subWorkerCompleted">New RunWorkerCompletedEventHandler(AddressOf onWorkerCompleted)</param>
-
''' <param name="SupportCancelation"></param>
-
''' <param name="StartWorkerNow">"False" to start the worker at a later time</param>
-
Public Sub CreateWorker(ByVal subDoWork As DoWorkEventHandler, Optional ByVal subProgressChanged As ProgressChangedEventHandler = Nothing, _
-
Optional ByVal subWorkerCompleted As RunWorkerCompletedEventHandler = Nothing, _
-
Optional ByVal SupportCancelation As Boolean = False, _
-
Optional ByVal StartWorkerNow As Boolean = True)
-
-
'* To use the background worker, you'll need to type in the arguments similar to what is shown:
-
-
'New DoWorkEventHandler(AddressOf subDoWork)
-
'New ProgressChangedEventHandler(AddressOf onProgressChanged)
-
'New RunWorkerCompletedEventHandler(AddressOf onWorkerCompleted)
-
-
' Instantiate the worker
-
Dim worker As New BackgroundWorker
-
-
' Tie in the DoWork event
-
AddHandler worker.DoWork, subDoWork ' Create delegate handle
-
-
' Tie in the ReportsProgress event
-
If subProgressChanged Is Nothing Then
-
worker.WorkerReportsProgress = False
-
Else
-
worker.WorkerReportsProgress = True
-
AddHandler worker.ProgressChanged, subProgressChanged ' Create callback handle
-
End If
-
-
' Tie in the RunWorkerCompleted event
-
If subProgressChanged IsNot Nothing Then AddHandler worker.RunWorkerCompleted, subWorkerCompleted ' Create completion handle
-
-
' Allow Cancelation
-
worker.WorkerSupportsCancellation = SupportCancelation
-
-
'* Start the worker
-
If StartWorkerNow = True Then StartWorker()
-
End Sub
-
-
''' <summary>Start or Re-start the background worker. If the worker is still running, this sub will do nothing.</summary>
-
Public Sub StartWorker()
-
If _worker.IsBusy = False Then _worker.RunWorkerAsync()
-
End Sub
-
-
''' <summary>Notify the worker that it should cancel. Will only work if the worker supports cancellation.</summary>
-
Public Sub CancelWorker()
-
If _worker.WorkerSupportsCancellation = True And _worker.IsBusy Then _worker.CancelAsync()
-
End Sub
-
-
''' <summary>Releases all resources used by the System.ComponentModel.Component</summary>
-
Public Sub Dispose()
-
_worker.Dispose()
-
End Sub
-
End Class
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fresh Air Rider |
last post by:
Hi
I understand that ASP.net 2.0 (Whidbey) is going to reduce coding by
70%.
Surely this is going to de-skill or dumb down the developer's task and
open up the task of web development to less...
|
by: Martin |
last post by:
I hope not, but, I think the answer to this question is "it can't be done".
Northwind sample database. Orders form.
Go to a new record.
Select a customer in "Bill To:"
Don't enter any...
|
by: Abhi |
last post by:
Hi..
I wanted the C source code in machine readable format for the book
"Numerical Recipes in C".
I got hold of the pdf version of the book somehow. Does anyone have the
complete C code of the...
|
by: JohnQ |
last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post).
It would be more than a little bit nice if C++ was much "cleaner" (less
complex) so that it wasn't a major world wide...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |