my goal are two methods:
public Task DoSthgAsync(CancellationToken ct);
and
public void DoSthgSync();
I wanted to do it this way:
private async Task<bool> DoSthgMixed(bool async, CancellationToken ct)
{
// … here some synchronous code
PrepareFileLoading();
// Here the switch(
if(async) await LoadFileAsync(ct);
else LoadFileSync();
// … here some more code (either sync or async or whatever)
// Return sthg.
return true;
}
public Task DoSthgAsync(CancellationToken ct)
{
return DoSthgMixed(true, ct);
}
public void DoSthgSync()
{
var task = DoSth.Mixed(false, CancellationToken.None);
var _ignore_result = task.Result;
}
So, the main principle is, to only call async methods in the async call and to only call sync methods in the sync call. This means it should be safe, to get the Result after the sync call, because no async call happened then.
My question: Does this result in safe behaviour or are there problems, if I do it this way. Is it allowed to call the Result of a Task, that does not do anything asynchronous?
My general problem is, that I have rather long methods, and try to avoid writing them twice.