Winforms异步委托 – VSTHRD101还是VSTHRD110?

huangapple go评论62阅读模式
英文:

Winforms async delegate - VSTHRD101 or VSTHRD110?

问题

我正在尝试在一个winforms应用程序中使用异步委托,但是还没有找到避免VSTHRD警告的方法,我想知道:

a) 有没有办法完全避免它们?或者
b) 哪一个是“较小的邪恶”,或者
c) 如果我接受一个警告而不是另一个,是否还会有异常处理的问题?

我的代码首先如下所示:

this.Load += async (sender, e) =>
{
...
await AsyncMethod();
...
}


当然,这会产生VSTHRD101警告:
VSTHRD101 避免不支持的异步委托

我按照https://github.com/Microsoft/vs-threading/issues/447 和https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD101.md中给出的模式进行了操作:

var factory = joinableTaskContext.Factory;

this.Load += (sender, e) => factory.RunAsync( async () =>
{
...
await AsyncMethod();
...
}


...现在我得到了VSTHRD110,因为RunAsync现在正在丢弃从RunAsync返回的Task:

VSTHRD110 观察异步调用的结果

我怀疑我可能在这里漏掉了什么!任何意见都受欢迎!
英文:

I'm trying to use async delegates in a winforms application, but haven't managed to find a way to avoid a VSTHRD warning, and I was wondering:

a) Is there a way to avoid them entirely? or
b) Which is the 'lesser evil', or
c) If I accept one warning over the other, is there still an issue with exception handling?

My code first looked as follows:

this.Load += async (sender, e) =>
{
...
    await AsyncMethod();
...
}

Of course, this gives the VSTHRD101 warning:
VSTHRD101 Avoid unsupported async delegates

I followed the pattern given in https://github.com/Microsoft/vs-threading/issues/447 and https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD101.md:

var factory = joinableTaskContext.Factory;

this.Load += (sender, e) => factory.RunAsync( async () =>
{
...
    await AsyncMethod(); 
...
}

...I now get VSTHRD110, as RunAsync is now discarding the returned Task from RunAsync:

VSTHRD110 Observe result of async calls

I suspect I must be missing something here! Any input is welcome!

答案1

得分: 1

VSTHRD100文档的底部,建议使用方法组语法作为参数时,可以定义具有所需签名但没有async修饰符的方法,并在方法内定义匿名委托或Lambda表达式。

private void HandleEvent(object sender, EventArgs e)
{
   _ = joinableTaskFactory.RunAsync(async () =>
   {
      // 异步代码
   });
}

请注意使用_来丢弃返回值(在此情况下是JoinableTask)。如果确实想要使用匿名方法,可以像这样做:

var factory = joinableTaskContext.Factory;

this.Load += (sender, e) => { _ = factory.RunAsync(async () =>
    {
    ...
        await AsyncMethod(); 
    ...
    });
};
英文:

At the bottom of the documentation for VSTHRD100, it suggests:

> When using method group syntax as an argument, you can define the method with the required signature, without the async modifier, and define an anonymous delegate or lambda within the method

private void HandleEvent(object sender, EventArgs e)
{
   _ = joinableTaskFactory.RunAsync(async () =>
   {
      // async code
   });
}

Notice the use of _ to discard the return value (a JoinableTask in this case). If you really want to use an anonymous method, you can probably do it like this:

var factory = joinableTaskContext.Factory;

this.Load += (sender, e) => { _ = factory.RunAsync( async () =>
    {
    ...
        await AsyncMethod(); 
    ...
    });
};

huangapple
  • 本文由 发表于 2023年5月18日 12:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定