In a console application. I need to load some long running code (network-stuff, REST calls) off my main thread. I want to pass it to a background thread and don't block the calling thread. I will invoke events in that method to handle it's result.
Is there any difference beetween doing this,
private async Task DoSomethingAsync() {
// Doing long running stuff
}
public async Task MainThreadAsync() {
_ = Task.Run(async () => await DoSomethingAsnyc());
// Continue with other stuff and don't care about DoSomethingAsync()
}
and doing this?
private async void DoSomethingAsync() {
// Doing long running stuff
}
public async Task MainThreadAsync() {
DoSomethingAsync();
// Continue with other stuff and don't care about DoSomethingAsync()
}
VB.Net:
Private Async Function DoSomethingAsync() As Task
' Doing long running stuff
End Function
Public Async Function MainThreadAsync() As Task
Task.Run(Async Function() As Task
Await DoSomethingAsync()
End Function)
' Continue with other stuff and don't care about DoSomethingAsync()
End Function
vs
Private Async Sub DoSomethingAsync()
' Doing long running stuff
End Sub
Public Async Function MainThreadAsync() As Task
DoSomethingAsync()
' Continue with other stuff and don't care about DoSomethingAsync()
}
Or is there even a better way? Also, is there a difference beetween c# and vb.net in this regard?