后台工作者,如何构造命令

发布于 2024-11-08 03:54:25 字数 345 浏览 0 评论 0原文

如果我有一个方法可以执行类似的操作

    if (ab)
    {
        //dostuff
    }
    else if (b)
    {
        //dostuff
    }
    else if (c)
    {
        //do stuff.
    }

并且每个方法都会关闭当前打开的表单,并重新显示具有不同数据的新表单。我怎样才能用一个显示加载栏的表单来包装每个内容,只是为了让用户没有加载任何内容。

我无法在新线程中打开加载表单,因为进度条没有进度,而且有 3 个不同的后台工作人员执行相同的操作,这似乎很愚蠢。

谢谢

If i have a method that does something like this

    if (ab)
    {
        //dostuff
    }
    else if (b)
    {
        //dostuff
    }
    else if (c)
    {
        //do stuff.
    }

And each closes the currently open form, and redisplays a new form with different data. How can i wrap each with a form that displays a loading bar just to let the user no something is loading.

I cant open the loading form in the new thread because the progress bar doesnt progress and it seems silly ot have 3 different background workers with do work methods which al ldo the same thing .

thanks

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

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

发布评论

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

评论(3

等待我真够勒 2024-11-15 03:54:25

您可以轻松地将数据传递到 BackgroundWorker

BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;

if (ab)
{
    //dostuff
    bw.RunWorkerAsync("ab"); // Run
}
else if (b)
{
    //dostuff
    bw.RunWorkerAsync("b"); // Run
}
else if (c)
{
    //do stuff.
    bw.RunWorkerAsync("c"); // Run
}

然后,有:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    string from = sender as string;

    switch case (string)
    {
        case "ab" : 
                   // process
                   break;
        case "b" : 
                   // process
                   break;
        case "c" : 
                   // process
                   break;
    }
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Add here your progress output
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Add here your completed output
}

you can easily pass data into the BackgroundWorker

BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;

if (ab)
{
    //dostuff
    bw.RunWorkerAsync("ab"); // Run
}
else if (b)
{
    //dostuff
    bw.RunWorkerAsync("b"); // Run
}
else if (c)
{
    //do stuff.
    bw.RunWorkerAsync("c"); // Run
}

and then, have:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    string from = sender as string;

    switch case (string)
    {
        case "ab" : 
                   // process
                   break;
        case "b" : 
                   // process
                   break;
        case "c" : 
                   // process
                   break;
    }
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Add here your progress output
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Add here your completed output
}
听,心雨的声音 2024-11-15 03:54:25

处理 ProgressChanged 事件 更新进度条?这将在 UI 线程上运行回调。

由于BackgroundWorker 通过DoWork 事件运行任务,因此在所有情况下都可以将其连接到相同的方法(或任意方法)。如果使用闭包(阅读:lambdas/anon 函数),则可以轻松保留正确的绑定。

BackgroundWorker bw = new BackgroundWorker();
// ... other setup such as a unified ProgressChanged

if (ab) {
    var greeting = "Hello";
    var name = "Fred";
    bw.DoWork += (sender, args) => {
        // this can be entirely unique or funnel to something unified.
        CallSomeSharedMethods();
        // use some bound variables (carefully)
        // closures are nifty, but watch mutations and lifetimes
        // (I find this generally easier than dealing with the DoWorkArgs data)
        Console.WriteLine(greeting + " " + name + "!");
        // update some progress
        bw.ReportProgress(100);
    };
} else {
    // likewise for others re-using as much
    // (or little) as required
}

快乐编码。

Handle the ProgressChanged event to update the progress bar? This will run the callback on the UI thread.

Since the BackgroundWorker runs the task through the DoWork event, this can be wired to the same method in all cases (or to an arbitrary method). If closures (read: lambdas/anon functions) are used, the correct binding can be trivially kept about.

BackgroundWorker bw = new BackgroundWorker();
// ... other setup such as a unified ProgressChanged

if (ab) {
    var greeting = "Hello";
    var name = "Fred";
    bw.DoWork += (sender, args) => {
        // this can be entirely unique or funnel to something unified.
        CallSomeSharedMethods();
        // use some bound variables (carefully)
        // closures are nifty, but watch mutations and lifetimes
        // (I find this generally easier than dealing with the DoWorkArgs data)
        Console.WriteLine(greeting + " " + name + "!");
        // update some progress
        bw.ReportProgress(100);
    };
} else {
    // likewise for others re-using as much
    // (or little) as required
}

Happy coding.

终难遇 2024-11-15 03:54:25

正如 balexandre 已经指出的,您可以将数据传递给 Worker。但这些数据也可以通过 Action 或 Func 来实现。因此,您可以传入应在后台工作人员中执行的任何方法。可以在此答案中找到示例。

但请注意,如果您尝试在后台工作任务中设置 gui 元素的任何属性,则可能会遇到跨线程异常。要完成此 BeginInvoke 是您的朋友,它也可以封装在 扩展方法

As balexandre already noted you can pass data to the Worker. But this data can also by an Action or Func. So you can pass in any method that should be performed in a background worker. An example can be found in this answer.

But be aware that you could run into Cross-Thread exceptions if you try to set any property of a gui element within the background worker task. To accomplish this BeginInvoke is your friend which can also be encapsulated in an extension method.

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