Reputation: 693
I'm new to Async functions and I'm learning it. I have written code that returns a value from an Async function and prints it to the console. Upon running the code a get System.Reflection.AmbiguousMatchException
on Dim result As Integer = Await task
and I don't know why. This is my code:
Module Module1
Sub Main()
Dim task = New Task(AddressOf testAync)
task.Start()
task.Wait()
End Sub
Async Sub testAync()
Dim task As Task(Of Integer) = HandleFileAsync(9000)
Dim result As Integer = Await task
Console.WriteLine(result)
Console.ReadLine()
End Sub
Async Function HandleFileAsync(ByVal x As Integer) As Task(Of Integer)
Return Await Task.Run(aFunction(x))
End Function
Public Function aFunction(ByVal intIn)
Return intIn
End Function
End Module
Upvotes: 1
Views: 226
Reputation: 1310
Your issue lies in HandleFileAsync
, where you are awaiting the running of the synchronous function aFunction
. I am not sure why you are not explicitly typing your parameter and return value in that function, but at the end of the day it doesn't really relate to your problem so I will ignore.
Since aFunction
is not asynchronous, you do not need to await it or create a task. Your error is that Task.Run(aFunction(x))
is expecting an asynchronous Action
, or a function that returns an asynchronous Task
.
Simply modify HandleFileAsync
to the following:
Async Function HandleFileAsync(ByVal x As Integer) As Task(Of Integer)
Return aFunction(x)
End Function
Upvotes: 3