如何将数组列表传递给后台工作者。 [英] How to pass array list to background worker.

查看:154
本文介绍了如何将数组列表传递给后台工作者。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个arraylist'searchtext'和另一个 arraylists str [] 的arraylist传递给backgroundworker1_DoWork,然后对它们执行操作。请帮忙。其他网站上的东西都很不清楚。我们也很欣赏以简单的方式解释这个概念的网页链接。

I need to pass one arraylist 'searchtext' and another arraylist of arraylists str[] to the backgroundworker1_DoWork and then perform operations on them. Please help. Stuff on other websites were very unclear. A link to a webpage explaining this concept in a simple fashion is also appreciated.

推荐答案

这很简单:

  • 将状态对象传递给 RunWorkerAsync 方法;
  • DoWork 事件处理程序中,传递 DoWorkEventArgs 类的实例。无论您将哪种对象作为状态传递给 RunWorkerAsync 方法,都可以从 Argument 属性中获取。
  • 由于 Argument 属性是对象,因此您需要将其强制转换为传入的类型。
It's quite simple:
  • Pass the state object to the RunWorkerAsync method;
  • In the DoWork event handler, you are passed an instance of the DoWorkEventArgs class. Whatever object you passed as the state to the RunWorkerAsync method is available from the Argument property.
  • Since the Argument property is an object, you'll need to cast it to the type you passed in.
private sealed class BackgroundWorkState
{
    public List<string> List1 { get; set; }
    public List<MyClass> List2 { get; set; }
    public string SomethingElse { get; set; }
}

private void StartBackgroundWork()
{
    BackgroundWorkState workerState= new BackgroundWorkState
    {
        List1 = ...,
        List2 = ...,
        SomethingElse = ...,
    };
    
    backgroundWorker.RunWorkerAsync(workerState);
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorkState myState = (BackgroundWorkState)e.Argument;
    // The myState variable is now the same object you passed to the RunWorkerAsync method.
    ...
}


将它传递给DoWorkAsync方法,并从eventargs中检索它:

You pass it to the DoWorkAsync method, and retrieve it from the eventargs:
    List<string> list = new List<string>(){"hello", "Goodbye"};
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerAsync(list);
    ...
    }

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    object o = e.Argument;
    List<string> list = o as List<string>;
    if (list != null)
        {
        ...
        }


而不是使用 BackgroundWorker ,你可以创建一个线程显式(使用构造函数)并根据需要重用它。



如果使用我介绍的线程包装器的概念很好地解决了与其他线程的数据交换的问题: br />
如何传递ref参数线程 [ ^ ],

更改线程(生产者)启动后的参数 [ ^ ],

C #MultiThreading [ ^ ]。



这样,你可以在包装器中隐藏线程同步细节;旁边,你在初始化时传递它的实例this。



这是你如何重用包装器的线程:

使代码线程安全 [ ^ ],

在webservice中运行一个永远不会被终止的作业/线程/进程(asp.net ) [ ^ ]。



(你可以在我上面提到的答案中找到一些相关的想法。)



-SA
Instead of using BackgroundWorker, you can create a thread explicitly (using a constructor) and reuse it as needed.

The problem if data exchange with other thread is solved nicely using the conception of thread wrapper I introduced:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^].

This way, you can hide thread synchronization detail inside a wrapper; beside, you pass its instance "this" at initialization.

This is how you can reuse the thread of the wrapper:
Making Code Thread Safe[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^].

(You can find some related ideas in my past answers referenced above.)

—SA


这篇关于如何将数组列表传递给后台工作者。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆