使用 Winforms 和 C# 继续更新文本框

Update text box on continuation with Winforms and C#

C# 菜鸟,来自其他语言的经验。 (最著名的是 Java)。

我正在查看 this question 的代码。这是 VS 2013 中的标准 WinForms C# 项目:

drop a button and a textbox on the form and use this code:

private void button1_Click(object sender, EventArgs e)
{
    Task.Factory.StartNew<int>(() => DelayedAdd(5, 10))
        .ContinueWith(t => DelayedAdd(t.Result, 20))
        .ContinueWith(t => DelayedAdd(t.Result, 30))
        .ContinueWith(t => DelayedAdd(t.Result, 50))
        .ContinueWith(t => textBox1.Text = t.Result.ToString(),
            TaskScheduler.FromCurrentSynchronizationContext());
}

private int DelayedAdd(int a, int b)
{
    Thread.Sleep(500);
    return a + b;
}

按下按钮,等待2秒让DelayedAdd四个调用完成,结果(115)显示在文本框中。 如何在每次 DelayedAdd 调用后将结果显示在文本框中?

我试图在每次调用之间插入最后的延续,

Task.Factory.StartNew<int>(() => DelayedAdd(5, 10))
    .ContinueWith(t => textBox1.Text = t.Result.ToString(),
        TaskScheduler.FromCurrentSynchronizationContext())

    .ContinueWith(t => DelayedAdd(t.Result, 20))
    .ContinueWith(t => textBox1.Text = t.Result.ToString(),
        TaskScheduler.FromCurrentSynchronizationContext())

    .ContinueWith(t => DelayedAdd(t.Result, 30))
    .ContinueWith(t => textBox1.Text = t.Result.ToString(),
        TaskScheduler.FromCurrentSynchronizationContext())

    .ContinueWith(t => DelayedAdd(t.Result, 50))
    .ContinueWith(t => textBox1.Text = t.Result.ToString(),
        TaskScheduler.FromCurrentSynchronizationContext());

但是失败了,我猜是因为我插入的延续没有 return 整数结果 t。我是一个 C# 菜鸟,我什至不知道如何修复 that,更不用说用惯用的方式来做这件事了。

知道了!如果有人有 better/more 个有趣的选择,请回答。

首先,我尝试了一个多行 lambda 来代替我的问题的错误延续:

    .ContinueWith(t => {
        textBox1.Text = t.Result.ToString();
        return t.Result;
    },
        TaskScheduler.FromCurrentSynchronizationContext()); 
    }

这有点冗长和重复,所以我把它擦干了:

    private void button1_Click(object sender, EventArgs e)
    {
        // SCHEDULING LOGIC
        Task.Factory.StartNew<int>(() => DelayedAdd(5, 10))
            .ContinueWith(t => UpdateText(t.Result),
                TaskScheduler.FromCurrentSynchronizationContext())

            .ContinueWith(t => DelayedAdd(t.Result, 20))
            .ContinueWith(t => UpdateText(t.Result),
                TaskScheduler.FromCurrentSynchronizationContext())

            .ContinueWith(t => DelayedAdd(t.Result, 30))
            .ContinueWith(t => UpdateText(t.Result),
                TaskScheduler.FromCurrentSynchronizationContext())

            .ContinueWith(t => DelayedAdd(t.Result, 50))
            .ContinueWith(t => UpdateText(t.Result),
                TaskScheduler.FromCurrentSynchronizationContext()); 
    }

    private int UpdateText(int i)
    {
        // UI LOGIC
        textBox1.Text = i.ToString();
        return i;
    }

    private int DelayedAdd(int a, int b)
    {
        // PROCESS LOGIC
        Thread.Sleep(500);
        return a + b;
    }

我只是更改 DelayedAdd 方法:

    private void button1_Click(object sender, EventArgs e)
    {
        Task.Factory.StartNew<int>(() => DelayedAdd(5, 10))
            .ContinueWith(t => DelayedAdd(t.Result, 20))
            .ContinueWith(t => DelayedAdd(t.Result, 30))
            .ContinueWith(t => DelayedAdd(t.Result, 50));
    }

    private int DelayedAdd(int a, int b)
    {
        if (textBox1.InvokeRequired)
        {
            textBox1.BeginInvoke((Action)(() => textBox1.Text = (a + b).ToString()));
        }
        Thread.Sleep(500);
        return a + b;
    }