后台工作者

发布于 2024-12-08 22:00:40 字数 337 浏览 0 评论 0原文

我制定了一种列出路径中所有 ftp 文件夹的方法。
我的问题是我需要将其作为数组返回。
但它是一个后台工作者,我只能将它返回到“RunWorkerCompleted”,
与 e.Result。
1)如何将参数传递给方法?(后台工作人员的)
2) 如何将文件夹数组返回到正常方法?
就像这样:

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

I have made a method that lists all of the ftp folders in a path.
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",
with e.Result.
1) How can I pass a parameter to the method?(of the background-worker)
2) How can I return the array of folders to a normal method?
Like so:

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

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

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

发布评论

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

评论(2

渔村楼浪 2024-12-15 22:00:40

您可以将列表作为表单本身的成员,并让工作人员在其工作时添加到其中(因为工作人员代码属于表单,因此该成员将在范围内)。

因此,如果您希望工作人员将其项目添加到列表的特定实例中;只需在触发工作人员之前先将其设置到成员中即可。

但是,您必须小心使用这种方法,不要在工作线程运行时访问该列表;因为它不是线程安全的。在 .Net 4 中,您可以使用 ConcurrentBag - 它是线程安全的。

如果您不想这样做,那么正如您所说,在 RunWorkerCompleted 事件中传递的事件参数的 Result 成员将用于传达结果 - 并且是您返回列表的地方。您还可以使用 ProgressChanged 事件 (在 MSDN 上)也可以流回结果的单个快照。

我不认为这是不合理的:异步操作与同步调用是根本不同的范例,您根本无法使用相同的调用和返回语义。

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).

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.

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. You can also use the ProgressChanged event (on MSDN) to stream back individual snapshot of results as well.

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.

可是我不能没有你 2024-12-15 22:00:40

您可以传递您的参数,因为它们是对象。 BackgroundWorker 采用object 参数。 object 的变量可以携带任何内容:数组、引用和变量。

示例:

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

在方法中:

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;
        }

当工作完成时:

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

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

You can pass your arguments as they are objects. BackgroundWorker takes a parameter of object. A variable of object can carry anything: array, references and variables.

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