Fail to call Sub from Async method
-
I need to call a method (returning void) in an async method. I have problem with the syntax of the Task.Run() in VB. I have a working example in C#. So it's just another: "Parse-this-line-from-C#-into-VB-and-it-should-work". VB-code
Module Module1
Sub Main() Console.WriteLine("Start of Run") Run() Console.WriteLine("End of Run") Console.ReadLine() End Sub Async Sub Run() Dim arg = "LEET 1337" Await Threading.Tasks.Task.Run(Function() Foo(arg)) ' This line fails... End Sub Sub Foo(ByVal asdf As String) Console.WriteLine("Start of Foo") Threading.Thread.Sleep(5000) Console.WriteLine(asdf) Console.WriteLine("End of Foo") End Sub
End Module
C#-code
using System;
using System.Threading;
using System.Threading.Tasks;namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start Run");
Run();
Console.WriteLine("End of Run");
Console.ReadLine();
}static async void Run() { var arg = "LEET 1337"; await Task.Run(() => Foo(arg)); // This line works. } static void Foo(string asdf) { Console.WriteLine("Start Foo"); Thread.Sleep(5000); Console.WriteLine(asdf); Console.WriteLine("End of Foo"); } }
}
-
I need to call a method (returning void) in an async method. I have problem with the syntax of the Task.Run() in VB. I have a working example in C#. So it's just another: "Parse-this-line-from-C#-into-VB-and-it-should-work". VB-code
Module Module1
Sub Main() Console.WriteLine("Start of Run") Run() Console.WriteLine("End of Run") Console.ReadLine() End Sub Async Sub Run() Dim arg = "LEET 1337" Await Threading.Tasks.Task.Run(Function() Foo(arg)) ' This line fails... End Sub Sub Foo(ByVal asdf As String) Console.WriteLine("Start of Foo") Threading.Thread.Sleep(5000) Console.WriteLine(asdf) Console.WriteLine("End of Foo") End Sub
End Module
C#-code
using System;
using System.Threading;
using System.Threading.Tasks;namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start Run");
Run();
Console.WriteLine("End of Run");
Console.ReadLine();
}static async void Run() { var arg = "LEET 1337"; await Task.Run(() => Foo(arg)); // This line works. } static void Foo(string asdf) { Console.WriteLine("Start Foo"); Thread.Sleep(5000); Console.WriteLine(asdf); Console.WriteLine("End of Foo"); } }
}
Try:
Await Threading.Tasks.Task.Run(Sub() Foo(arg))
Lambda Expressions - Visual Basic | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try:
Await Threading.Tasks.Task.Run(Sub() Foo(arg))
Lambda Expressions - Visual Basic | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer