For asynchronous delegates in the code, I do the following everywhere:
public class SomeCaller
{
public event Action SomeChanged;
public event Func<Task> SomeChangedAsync;
//If in Caller async method
public async Task SomeMethodAsync()
{
SomeChanged?.Invoke();
if (SomeChangedAsync != null)
await SomeChangedAsync();
}
//if in Caller synchronous method
public void SomeMethod()
{
SomeChanged?.Invoke();
if (SomeChangedAsync != null)
Task.Run(async () => await SomeChangedAsync());
}
}
Is there any point in such a solution (to separate the event for async) or is this an example of poor design? If this is bad, then I would like to understand why and how best to call async delegates?