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. Call a method inside a Task.Run and return the value

Call a method inside a Task.Run and return the value

Scheduled Pinned Locked Moved Visual Basic
helpquestion
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 have a method Bar. Bar take some arbitrary amount of time to complete. I want to call Bar from a method Foo. Foo run some code sync and last run Bar async. Bar returns a value and I want Foo 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 from Foo only return the value from Bar?

    L 2 Replies Last reply
    0
    • M Mc_Topaz

      I have a method Bar. Bar take some arbitrary amount of time to complete. I want to call Bar from a method Foo. Foo run some code sync and last run Bar async. Bar returns a value and I want Foo 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 from Foo only return the value from Bar?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You do not make direct calls to Async functions, but call them via Await, as described in Asynchronous Programming with Async and Await - Visual Basic | Microsoft Docs[^].

      1 Reply Last reply
      0
      • M Mc_Topaz

        I have a method Bar. Bar take some arbitrary amount of time to complete. I want to call Bar from a method Foo. Foo run some code sync and last run Bar async. Bar returns a value and I want Foo 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 from Foo only return the value from Bar?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        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