长时间运行时如何更新UI(文本字段)? [英] How to update UI (text fields) during long running function?

查看:39
本文介绍了长时间运行时如何更新UI(文本字段)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能没有道理,而且我正艰难地尝试着想办法加以解释,因此我将向您展示一段代码来提供帮助.我在Visual Studio Express 2010上使用Winforms:

I know that the question may not make sense, and I'm having a tough time trying to think of a way to explain it, so I will show a snippet of code to help. I'm using Winforms on visual studio express 2010:

private void button1(object sender, EventArgs e)
    {
        txtOutput.Text += "Auto-collecting variables. This may take several minutes";
        string v = foo();
        txtOutput.Text += "\n" + v;
        string b = bar();
        txtOutput.Text += "\n" + b;

        txtOutput.SelectionStart = txtOutput.Text.Length;
        txtOutput.ScrollToCaret(); //scrolls to the bottom of textbox
    }

因此,基本上,当用户单击button1时,我希望在文本框中显示自动收集变量...",然后执行foo(),显示该内容,然后执行bar(),并且然后显示出来.

So basically, when the user clicks button1, I want "Auto-collecting variables..." to be displayed in the textbox, and then have foo() execute, display that, and then have bar() execute, and then display that.

当前正在发生的事情是执行foo()和bar(),然后在执行foo()和bar()之后一次全部显示所有内容(功能需要几分钟).反正有什么解决办法,还是有解决办法?

What is currently happening is that foo() and bar() execute, and then everything is displayed all at once after foo() and bar() have executed (functions that take several minutes). Is there anyway to fix this, or is there a work around?

C#的版本为4.0.如果我更新到4.5或5.0,没有.NET 4.5/5.0的计算机是否可以运行.exe?

Version of C# is 4.0. If I update to 4.5 or 5.0, will computers without .NET 4.5/5.0 be able to run the .exe?

推荐答案

C#5.0做到了这一点.

C# 5.0 makes doing this trivial.

使用 Task.Run 在后台线程中执行长时间运行的任务,并使用 await 在UI线程中继续执行该方法的其余部分,而不会阻塞异步任务期间的UI线程.

Execute the long running tasks in a background thread using Task.Run and use await to execute the rest of the method as a continuation in the UI thread without blocking the UI thread for the duration of the asynchronous task.

private async void button1(object sender, EventArgs e)
{
    txtOutput.Text += "Auto-collecting variables. This may take several minutes";
    string v = await Task.Run(() => foo());
    txtOutput.Text += "\n" + v;
    string b = await Task.Run(() => bar());
    txtOutput.Text += "\n" + b;

    txtOutput.SelectionStart = txtOutput.Text.Length;
    txtOutput.ScrollToCaret(); //scrolls to the bottom of textbox
}

您可以在C#4.0中执行以下操作:(第一个解决方案将由编译器转换为类似的内容.)

You can do the same in C# 4.0 like so: (The first solution will be transformed by the compiler into something similar.)

private  void button1(object sender, EventArgs e)
{
    txtOutput.Text += "Auto-collecting variables. This may take several minutes";
    Task.Factory.StartNew(() => foo())
        .ContinueWith(t => txtOutput.Text += "\n" + t.Result
            , TaskScheduler.FromCurrentSynchronizationContext())
        .ContinueWith(t => bar())
        .ContinueWith(t =>
        {
            txtOutput.Text += "\n" + t.Result;
            txtOutput.SelectionStart = txtOutput.Text.Length;
            txtOutput.ScrollToCaret(); //scrolls to the bottom of textbox
        }
            , TaskScheduler.FromCurrentSynchronizationContext());
}

这篇关于长时间运行时如何更新UI(文本字段)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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