Call a method inside a Task.Run and return the value
-
I have a method
Bar
. Bar take some arbitrary amount of time to complete. I want to callBar
from a methodFoo
.Foo
run some code sync and last runBar
async.Bar
returns a value and I wantFoo
to return the same value. Here is the code to reproduce the behavior:Module Module1
Sub Main() Dim i As Integer = Foo() Console.Write("Enter anything: ") Console.ReadLine() Console.WriteLine(i) End Sub Async Function Foo() As Task(Of Integer) Console.WriteLine("Do something sync") Console.WriteLine("Do something sync") Return Await Task.Run(Function() Bar()) End Function Function Bar() As Integer Console.WriteLine("Bar Start") Threading.Thread.Sleep(2000) Console.WriteLine("Bar Stop") Return 5 End Function
End Module
But I get a compile error in the Main method:
Dim i As Integer = Foo()
Value of type Task(Of Integer) cannot be converted to Integer.
I understand the error, but I don't understand how I should solve it. I tried this line:Dim i As Integer = Foo().Result
Which compiles and runs. But the call to the Bar method ran sync, not async. How should I alter the code to run
Bar
async and fromFoo
only return the value fromBar
? -
I have a method
Bar
. Bar take some arbitrary amount of time to complete. I want to callBar
from a methodFoo
.Foo
run some code sync and last runBar
async.Bar
returns a value and I wantFoo
to return the same value. Here is the code to reproduce the behavior:Module Module1
Sub Main() Dim i As Integer = Foo() Console.Write("Enter anything: ") Console.ReadLine() Console.WriteLine(i) End Sub Async Function Foo() As Task(Of Integer) Console.WriteLine("Do something sync") Console.WriteLine("Do something sync") Return Await Task.Run(Function() Bar()) End Function Function Bar() As Integer Console.WriteLine("Bar Start") Threading.Thread.Sleep(2000) Console.WriteLine("Bar Stop") Return 5 End Function
End Module
But I get a compile error in the Main method:
Dim i As Integer = Foo()
Value of type Task(Of Integer) cannot be converted to Integer.
I understand the error, but I don't understand how I should solve it. I tried this line:Dim i As Integer = Foo().Result
Which compiles and runs. But the call to the Bar method ran sync, not async. How should I alter the code to run
Bar
async and fromFoo
only return the value fromBar
? -
I have a method
Bar
. Bar take some arbitrary amount of time to complete. I want to callBar
from a methodFoo
.Foo
run some code sync and last runBar
async.Bar
returns a value and I wantFoo
to return the same value. Here is the code to reproduce the behavior:Module Module1
Sub Main() Dim i As Integer = Foo() Console.Write("Enter anything: ") Console.ReadLine() Console.WriteLine(i) End Sub Async Function Foo() As Task(Of Integer) Console.WriteLine("Do something sync") Console.WriteLine("Do something sync") Return Await Task.Run(Function() Bar()) End Function Function Bar() As Integer Console.WriteLine("Bar Start") Threading.Thread.Sleep(2000) Console.WriteLine("Bar Stop") Return 5 End Function
End Module
But I get a compile error in the Main method:
Dim i As Integer = Foo()
Value of type Task(Of Integer) cannot be converted to Integer.
I understand the error, but I don't understand how I should solve it. I tried this line:Dim i As Integer = Foo().Result
Which compiles and runs. But the call to the Bar method ran sync, not async. How should I alter the code to run
Bar
async and fromFoo
only return the value fromBar
?In C#, it would be. var something = await Foo(); int i = something.Result; in this case "var", is the same as Task at compile time.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food