I've got method which I want to run asynchronously:
public async Task Send(MemoryMapModel memoryMap, SerialPort serialPort, ProgressBar progressBar, Settings settings)
{
SendFrames(memoryMap, serialPort, progressBar, _token.Token, settings);
}
And I use it like this by Task.Run():
await Task.Run(async () => await _rtuLogic.Send(memoryMap, serialPort, pbProgressBar, settings).ConfigureAwait(false));
Method "Send" throws exceptions which I want to catch, but to do that, it has to be async Task, but there are no awaitable methods and VS wants me not to use async keyword.
Why can't I catch exceptions from Task.Run(() => void)
but it do works for Task.Run(async () => async Task)
, though there are no awaitable methods?
EDIT: Also when I tried
Task.Factory.StartNew(() => { _rtuLogic.SendAsync(memoryMap, serialPort, pbProgressBar, settings); });
I can't catch and handle exceptions. If I don't use Task.Run(...) Send doesn't run asynchronously and freezes GUI.