小编ICl*_*ble的帖子

如何暂停在工作线程上运行的任务并等待用户输入?

如果我有一个在工作线程上运行的任务并且当它发现问题时,是否可以暂停并等待用户干预然后再继续?

例如,假设我有这样的事情:

async void btnStartTask_Click(object sender, EventArgs e)
{
    await Task.Run(() => LongRunningTask());
}

// CPU-bound
bool LongRunningTask()
{
    // Establish some connection here.

    // Do some work here.

    List<Foo> incorrectValues = GetIncorrectValuesFromAbove();

    if (incorrectValues.Count > 0)
    {
        // Here, I want to present the "incorrect values" to the user (on the UI thread)
        // and let them select whether to modify a value, ignore it, or abort.
        var confirmedValues = WaitForUserInput(incorrectValues);
    }
    
    // Continue processing.
}
Run Code Online (Sandbox Code Playgroud)

是否可以WaitForUserInput()用在 …

.net c# winforms async-await

6
推荐指数
1
解决办法
539
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1

winforms ×1