Running a class as a new thread

I want to start work in a new thread or use a background worker to do this, but have not done this before and asked you how I should do it.

My program has a datagridview with a list of files, one file per line. I want the user to be able to select a line and then click “Start Download” to start the background download task. I want events to return to download progress.

I have a clsDownload class that handles everything and returns events back, but how do I execute forex?

Should I use System.ComponentModel.BackgroundWorker inside the class or create some kind of wrapper that processes this or uses some other streaming data?

Thanks.

Edit: I don't understand how to implement my loading in backgroundworker, any small example would be very nice. The msdn example didn't get me far.

I have a download class that has a StartDownload function. Should I use a flashlight in class or in the caller? "feeling stupid"

+4
source share
5 answers

I created several different classes that include BackgroundWorker. I usually use the BackgroundWorker component in a form that will open when the task is completed, and then pass this instance to the constructor of my work class.

Here's what your work class looks like:

Private m_bwMain As BackgroundWorker Public Sub New(ByVal bwMain As BackgroundWorker) m_bwMain = bwMain 'additional setup code here End Sub 

To start the task, you will do something similar in the Click event handler of the Download button:

 lblStatus.Text = "Initializing ..." bgwMain.RunWorkerAsync(someFileName) 

I declare my work class as a private member of the current form, and then create an instance in the BackgroundWorker.DoWork event. From there, you can call your method to download the file:

 Private Sub bgwMain_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwMain.DoWork m_oJobEngine = New JobEngine(CType(sender, BackgroundWorker)) m_oJobEngine.DownloadFile(CStr(e.Argument)) End Sub 

To report progress to the user, you can handle the events raised by your class in your main form. You just need to make sure the job class object declaration has the WithEvents keyword. From these handlers, you can call the ReportProgress BackgroundWorker method. From ReportProgress, you can make any changes you need for the user interface to indicate progress. Here is an example:

 Private Sub m_oJobEngine.DownloadProgress(ByVal bgw as Backgroundworker, ByVal bytesTransferred as Long) Handles m_oJobEngine.DownloadProgress bgw.ReportProgress(0, bytesTransferred) End Sub Private Sub bgwMain_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwMain.ProgressChanged lblStatus.Text = CLng(e.UserState).ToString & " bytes transferred." End Sub 

Hope this helps.

+1
source

I'm only going to do the loading and don't need other asynchronous processing, you can just use the asynchronous WebClient methods. Although, since you already have your own class, this is probably not the solution for you.

Otherwise, you can use BackgroundWorker , as you mentioned. The MSDN page has an example of how to do this.

EDIT: Story:

  • You create a BackgroundWorker from the caller,
  • When you want to start the background, you call BackgroundWorker.RunWorkerAsync ;
  • in the DoWork event DoWork you execute a wallpaper; in your case, you start your loading class;
  • while you are doing the wallpaper, you need to check CancelationPending from time to CancelationPending ;
  • when you want to report some kind of progress, you need to calculate it as a percentage and call ReportProgress .

And if you need something really customized, you can always create your own Thread .

I personally stuck to BackgroundWorker . It has a good set of notifications for various stages of work. If you use Thread , you will have to implement them yourself.

I would also like to make sure that the code does not create too many instances. You want to limit the number of related downloads and queue everything that happened in the past.

+3
source

I highly recommend BackgroundWorker if you need to provide user feedback in the user interface. The ProgressChanged and RunWorkerCompleted are RunWorkerCompleted in the user interface thread, so there is no need to do marshalling, which can make your code more complex.

+3
source

A background worker looks as if it should work ... MSDN has an example.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Or you could do something like:

 WaitCallBack workCallBack= new WaitCallBack(DownloadMethod); if(!ThreadPool.QueueUserWorkItem(workCallBack, "ThreadPooled") { // Unable to Pool } // Work has been added to pool and will execute when possible 

Depends on the options you need for Thread.

0
source

A class that uses clsDownload (perhaps your Form class) should use BackgroundWorker to run the load method.

0
source

All Articles