How to use BackGroundWorker in a class file?

My program.cs calls the parent mdi frmMain. FrmMain then opens up various child forms based on user action.

All processing logic is written in BusinessLogic.cs. frmMain at load calls the BusinessLogic.cs methods to initially populate the data. The child also calls BusinessLogic to retrieve data and process it. I would like to do all this through BackGroundWorker, i.e. FrmMain calls (for example) the StartBGWorker () method of BusinessLogic.cs, and this method creates a background worker for this particular call, raises the Do_work event, fetch and process, and closes when done.

I do not understand how to instantiate and events for backgroundworker. So how exactly do I use backgroundworker in the class file?

Edit: Perhaps my logic is wrong, and I should add BGW for each form that calls BusinessLogig.cs. Then whenever I call the BusinessLogic method, I can do this through the backgroundworker. Would it be better to implement?

Edit2: Feel a little idiotic about my doubts now that I have found a way. I just created a public static method with the initial BGW code in BusinessLogic. Now that I need to do the processing, I first call this method from my forms. I would like to know if my implementation of BGW is standard or is there a way to improve the design.

+4
source share
3 answers

Include:

using System.ComponentModel; 

Define this in your class:

 private BackgroundWorker BackgroundWorker = new BackgroundWorker(); 

Initialize it:

 BackgroundWorker.WorkerSupportsCancellation = false; BackgroundWorker.WorkerReportsProgress = true; BackgroundWorker.DoWork += BackgroundWorker_DoWork; BackgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged; BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundWorker_RunWorkerCompleted ); 

To start:

 BackgroundWorker.RunWorkerAsync(); 

Usage is exactly the same as you know from Windows Forms.

+3
source

You should take a look at the parallel task library. There you can run, link and synchronize asynchronous operations.

http://msdn.microsoft.com/en-us/library/dd537609.aspx

You can also enter an object with a delegate, an event, Actio, which can be called from a new task to report the progress

eg

You realize an entity like this

  public class TaskData { private Action<int> Callback { get; private set; } public TaskData(Action<int> callbackAction) { this.Callback = callbackAction; } public void TriggerCallBack(int percentageComplete) { Action<int> handler = Callback; if (handler != null) { handler(percentageComplete); } } } 

How do you create a task with this object as a parameter

 TaskData data = new TaskData((percentage)=> Console.WriteLine(percentage + "% completed")); Task myTask = new Task(new Action<object>((para)=> { ((TaskData)para).TriggerCallBack(4); } ),data); myTask.Start(); Console.ReadLine(); 

PS: The code is just a quick and dirty hack. feel free to improve it :-)

+1
source

Please do not use System.ComponentModel.BackgroundWorker , instead create your own threads and execute events if you want to report progress. I had so many mistakes that I can freely say that it should never be used.

-4
source

Source: https://habr.com/ru/post/892662/


All Articles