简体   繁体   中英

Compiler error when calling async Sub with TaskFactory

A helper method to run Async calls in a synchronised way throws a compiler error.

VB.NET, VisualStudio 2019, .NET Framework 4.6, Link to compiler error: https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc31143

Private Shared ReadOnly _taskFactory As TaskFactory = New TaskFactory(System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default)
Shared Sub RunSync(ByVal func As Func(Of Task))
    _taskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult()
End Sub

Call:

CLHelper.RunSync(Sub() test())

Method:

Async Sub test()
    Await doSomethingAsync()
End Sub

CLHelper.RunSync(Sub() test()) brings the compiler error BC36680: Method '' does not have a signature compatible with delegate <'delegatename'>

You may change test method to be Function , to reconcile it's signature with RunSync call:

Async Function test() As Task
    Await doSomethingAsync()
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM