If the function does not return a value, then try Wait:
Task.Delay(4000).Wait()
If it returns a result, then try this:
Dim r = SomeAsyncFunction().Result
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a VB Winforms application that uses Await in Form1_Load and works fine. I would now like to switch this to being a console application. How do i call asynchronous subs or functions from Main?
If the function does not return a value, then try Wait:
Task.Delay(4000).Wait()
If it returns a result, then try this:
Dim r = SomeAsyncFunction().Result
[Ran out of characters, continued here.]
So, i tried this in my actual project and it failed. The actual project is a WinForms app that can be compiled as an EXE or DLL, simply by changing the project type, and both work fine. I now tried compiling it as a console app, and by changing the signature to Form1_Load, calling the same calls, which is where the issue came up. After a bunch of tests, i have found it fails on Await if i instantiate a form. For example:
It hits the Await and hangs there. However, if i comment out the Dim statement, it works just fine! So, i tried using Wait() instead like you suggested, and that works just fine, though that requires turning it back into a Sub, as there is no return.
Character limit hit again, will continue in next comment.
In my case, however, i am using HttpClient. Wait() does indeed wait:
But how do i get the reply? Changing the line to:
Redlines Client.GetAsync("http://example.com").Wait(), with:
Edit: Oh, i see. I need GetAwaiter.GetResult. That works.
Does this mean i have to get rid of every Async/Await? Hmm, i wonder what the performance hit would be,
Try
Sign in to comment