相关疑难解决方法(0)

为什么无效async坏?

所以我理解为什么从异步中返回void通常没有任何意义,但我遇到了一种我认为完全有效的情况.考虑以下人为的例子:

protected override void OnLoad(EventArgs e)
{
    if (CustomTask == null)
        // Do not await anything, let OnLoad return.
        PrimeCustomTask();
}
private TaskCompletionSource<int> CustomTask;

// I DO NOT care about the return value from this. So why is void bad?
private async void PrimeCustomTask()
{
    CustomTask = new TaskCompletionSource<int>();
    int result = 0;
    try
    {
        // Wait for button click to set the value, but do not block the UI.
        result = await CustomTask.Task;
    }
    catch
    {
        // Handle exceptions …
Run Code Online (Sandbox Code Playgroud)

c# async-await

11
推荐指数
2
解决办法
7660
查看次数

标签 统计

async-await ×1

c# ×1