473,612 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Outsourcin g" Background worker creation

36 New Member
I am trying to code a simple background workers class without needing to use the backgroundworke r 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 onProgressChang ed below and use (Byval onWorkSub as SomeKindOfObjec t?, Byval onProgressChang edSub as SomeKindOfObjec t?) 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()
Jun 10 '09 #1
1 2024
Infog
36 New Member
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
Jun 11 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

32
2235
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 qualified and trained staff. Tell me if I'm wrong.
8
2040
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 products whatsoever. Now click or page up/down away from this new record. You just created a new order without a single item having been ordered. Not something a user should be able to do.
23
15654
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 book?. If yes,..can you please mail me the code or somehow share it with me?. With Regards, Abhishek S
169
9041
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 untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to embark on creating a nice little Small C++ compiler devoid of C++ language features that make...
0
8162
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8105
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4045
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.