Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Fail to call Sub from Async method

Fail to call Sub from Async method

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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");
        }
    }
    

    }

    R 1 Reply Last reply
    0
    • M Mc_Topaz

      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");
          }
      }
      

      }

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • R Richard Deeming

        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

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        Of course I have to switch Function() to Sub() :doh: Thank you for the correction!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups