How to use BackGroundWorker in class file?

女生的网名这么多〃 提交于 2019-12-06 06:08:34

问题


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

All the processing logic is written in BusinessLogic.cs. frmMain at load calls the methods of BusinessLogic.cs to initially populate the data. The child forms too call BusinessLogic to fetch data and process it. I'd like to do all this through a BackGroundWorker ie the frmMain calls the (say) StartBGWorker() method of BusinessLogic.cs and this method creates a backgroundworker for this specific call, calls the Do_work event, does the fetching & processing and closes when done.

I'm at a loss about how to create the instance and the events for backgroundworker. So how exactly do I use backgroundworker in a class file?

Edit: Maybe my logic is wrong and I should add a BGW to each form that calls BusinessLogig.cs. Then whenever I call a method of BusinessLogic I can do so through backgroundworker. Would this be a better implementation?

Edit2: Feel a bit idiotic about my doubt now that I found a way. I just created a public static method with the initialize code of BGW in BusinessLogic. Now whenever I need to do processing I first call this method from my forms. I'd like to know if my implementation of BGW is standard or is there any way to improve the design.


回答1:


Include:

using System.ComponentModel;

Define it 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 it:

BackgroundWorker.RunWorkerAsync();

The usage is exactly the same as you know from Windows Forms.




回答2:


You should have a look at the Parallel Task Library. There you can start,link and sync asynchronous operations.

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

You could also inject an object with an delegate, event, Actio which can be triggered from within the new task to report the progress

for example

You Implement 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);
            }
        }
    }

Than you create a Task with this entity as 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 :-)




回答3:


Please don't use System.ComponentModel.BackgroundWorker, create your own threads instead and implement events if you want to report progress. I had so much bugs with it that I can freely say that it should never ever be used.



来源:https://stackoverflow.com/questions/6664246/how-to-use-backgroundworker-in-class-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!