带参数的BackgroundWorker基本用法

发布于 2024-11-03 21:37:48 字数 240 浏览 6 评论 0原文

我想要在后台线程中执行的进程密集型方法调用如下所示:

object.Method(paramObj, paramObj2);

所有这三个对象都是我创建的。现在,从我看到的最初示例来看,您可以将一个对象传递到后台工作者的 DoWork 方法中。但是,如果我需要向该对象传递其他参数(就像我在这里所做的那样),我应该如何去做呢?我可以将其包装在一个对象中并完成它,但我认为获取其他人对此的输入是明智的。

My process intensive method call that I want to perform in a background thread looks like this:

object.Method(paramObj, paramObj2);

All three of these objects are ones I have created. Now, from the initial examples I have seen, you can pass an object into a backgroundworker's DoWork method. But how should I go about doing this if I need to pass additional parameters to that object, like I'm doing here? I could wrap this in a single object and be done with it, but I thought it would be smart to get someone else's input on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄人书 2024-11-10 21:37:48

您可以将任何对象传递到 RunWorkerAsync 调用的对象参数中,然后从 DoWork 事件中检索参数。您还可以使用 DoWorkEventArgs 中的 Result 变量将参数从 DoWork 事件传递到 RunWorkerCompleted 事件。

    public Form1()
    {
        InitializeComponent();

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

        object paramObj = 1;
        object paramObj2 = 2;

        // The parameters you want to pass to the do work event of the background worker.
        object[] parameters = new object [] { paramObj, paramObj2 };

        // This runs the event on new background worker thread.
        worker.RunWorkerAsync(parameters);
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        object[] parameters = e.Argument as object[];

        // do something.

        e.Result = true;
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        object result = e.Result;

        // Handle what to do when complete.                        
    }

You can pass any object into the object argument of the RunWorkerAsync call, and then retrieve the arguments from within the DoWork event. You can also pass arguments from the DoWork event to the RunWorkerCompleted event using the Result variable in the DoWorkEventArgs.

    public Form1()
    {
        InitializeComponent();

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

        object paramObj = 1;
        object paramObj2 = 2;

        // The parameters you want to pass to the do work event of the background worker.
        object[] parameters = new object [] { paramObj, paramObj2 };

        // This runs the event on new background worker thread.
        worker.RunWorkerAsync(parameters);
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        object[] parameters = e.Argument as object[];

        // do something.

        e.Result = true;
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        object result = e.Result;

        // Handle what to do when complete.                        
    }
も星光 2024-11-10 21:37:48

我想到的最简单的方法是为您拥有的每个对象创建一个具有属性的类,然后传递该属性。

public class MyWorkerContext
{
   public Object1 Worker;
   public Object2 Parameter1;
   public Object2 Parameter2;
}

然后在 DoWork 中您只需执行以下操作:

MyWorkerContext context = e.Argument as MyWorkerContext;

context.Worker.Method(context.Parameter1, context.Parameter2);

The simplest way that comes to my mind would be creating a class with a property for every object you have, and passing that.

public class MyWorkerContext
{
   public Object1 Worker;
   public Object2 Parameter1;
   public Object2 Parameter2;
}

then in the DoWork you simply do something like:

MyWorkerContext context = e.Argument as MyWorkerContext;

context.Worker.Method(context.Parameter1, context.Parameter2);
白况 2024-11-10 21:37:48

您可以在 DoWork 的 lambda 中捕获它们:

bgw.DoWork += (sender, e) => object.Method(paramObj, paramObj2) ;

e.Argument(即传递给 BackgroundWorker.RunWorkerAsync() 的状态值或对象)可以是 3 个中的 1 个,但是它是 System.Object 类型并且需要装箱。直接在 lambda 中传递所有参数可为您提供所有参数的完全类型安全性,无需装箱或转换。

You can capture them in a lambda for DoWork:

bgw.DoWork += (sender, e) => object.Method(paramObj, paramObj2) ;

e.Argument (i.e. the state value or object passed to BackgroundWorker.RunWorkerAsync()) could be 1 of the 3, but it is of type System.Object and would require boxing. Passing all of the parameters directly in the lambda gives you full type-safety on all of the parameters without any need for boxing or casting.

二智少女猫性小仙女 2024-11-10 21:37:48

您可以将对象传递到BackgroundWorker.RunWorkerAsync() 方法中。您应该将两个 paramObjs 包装到一个对象中(您可以使用数组、元组或您编写的其他复合类)并将其作为参数传递到 RunWorkerAsync() 中。

然后,当引发 DoWork 事件时,您可以通过查看事件处理程序的 DoWorkEventArgs 参数的 Argument 属性来检索该值。

有关完整示例,请查看 MSDN:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=VS.90%29.aspx

You can pass an Object into the BackgroundWorker.RunWorkerAsync() method. You should wrap your two paramObjs into one Object (you could use an array, a tuple, or some other composite class that you would write) and pass that as the argument into RunWorkerAsync().

Then, when your DoWork event is raised, you can retrieve this values by looking at the Argument property of the DoWorkEventArgs parameter of the event handler.

For a complete example, check out on MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=VS.90%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文