From Async/Await - Best Practices in Asynchronous Programming document, suggest prefer async Task methods over async void methods.
OnStart()
is not an event handler, override this method to perform actions when the application starts.
As a work around you can create your own custom event and handler that will allow for an async void
to be performed on your event handler. You will subscribe to the event when OnStart
is invoked by the application and then raise the custom event to be handled asynchronously.
private event EventHandler Starting = delegate { };
protected override void OnStart()
{
//subscribe to event
Starting += onStarting;
//raise event
Starting(this, EventArgs.Empty);
}
private async void onStarting(object sender, EventArgs args)
{
//unsubscribe from event
Starting -= onStarting;
//perform non-blocking actions
await checksubscriptionAsync();
}
private async Task checksubscriptionAsync()
{
await ...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…