简体   繁体   English

后台工作者

[英]Background workers

I have made a method that lists all of the ftp folders in a path. 我已经制定了一种列出路径中所有ftp文件夹的方法。
My problem is that I need to return this as an array. 我的问题是我需要将此作为数组返回。
But it is a background worker, and I can only return it to the "RunWorkerCompleted", 但这是后台工作人员,我只能将其返回到“ RunWorkerCompleted”,
with e.Result. 与e.Result。
1) How can I pass a parameter to the method?(of the background-worker) 1)如何将参数传递给方法?(背景工作者的)
2) How can I return the array of folders to a normal method? 2)如何将文件夹数组恢复为常规方法?
Like so: 像这样:

private void btnOpen_Click_1(object sender, EventArgs e)
        {
           string[]/List... a=  getDirectories(**path**)?
        }

You can have a List as a member of the form itself, and have the worker add into that as it does it's work (since the worker code will belong to the form, the member will be in scope). 您可以将List作为表单本身的成员,并在它的工作过程中添加worker(由于worker代码将属于表单,因此该成员将在范围内)。

Therefore if you want the worker to add its items into a specific instance of a list; 因此,如果您希望工作人员将其项目添加到列表的特定实例中,请执行以下操作: just set that into the member first before triggering the worker. 只需在触发工作程序之前先将其设置为成员。

However you must be careful with this approach not to access that list whilst the worker is running; 但是,您必须谨慎使用此方法,以免在工作程序运行时访问该列表。 as it's not thread safe. 因为它不是线程安全的。 In .Net 4 you could use the ConcurrentBag - which is thread-safe. 在.Net 4中,您可以使用ConcurrentBag-这是线程安全的。

If you don't want to do that, then the Result member of the event arguments passed in the RunWorkerCompleted event is, as you say, used to communicate results - and that is where you would return your list. 如果您不想这样做,那么可以按照您所说的,在RunWorkerCompleted事件中传递的事件参数的Result成员用于传递结果-这就是您返回列表的地方。 You can also use the ProgressChanged event ( on MSDN ) to stream back individual snapshot of results as well. 您还可以使用ProgressChanged事件( 在MSDN上 )也可以流回结果的单个快照。

I don't think this is unreasonable: async operations are a fundamentally different paradigm to synchronous calls, you simply can't use the same call-and-return semantics. 我不认为这是不合理的:异步操作是与同步调用根本不同的范例,您根本不能使用相同的调用返回语义。

You can pass your arguments as they are objects. 您可以传递参数,因为它们是对象。 BackgroundWorker takes a parameter of object . BackgroundWorker采用object的参数。 A variable of object can carry anything: array, references and variables. object的变量可以携带任何东西:数组,引用和变量。

Example: 例:

object[] args = new object[] { fileName, ....... };
this.backgroundWorker1.RunWorkerAsync(args);

in the method: 在方法中:

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            object o = e.Argument;
            object[] args = (object[])o;
            string fileName = (string)args[0];
            ....
            object[] result = ....
            e.Result = result;
        }

When the work has completed: 工作完成后:

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            object o = e.Result;
            object[] result = (object[])o;
            ...
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM