Created
June 25, 2017 21:50
-
-
Save thenameless314159/fb38a1aefe16da103e99374f61ca1994 to your computer and use it in GitHub Desktop.
experimental async entry point for wpf app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Entry Point of the App | |
/// </summary> | |
public partial class App | |
{ | |
private CancellationTokenSource _mainTokenSource; | |
private TaskScheduler _mainScheduler; | |
public TaskFactory MainFactory() | |
=> new TaskFactory( | |
_mainTokenSource.Token, | |
TaskCreationOptions.None, | |
TaskContinuationOptions.None, | |
_mainScheduler); | |
protected override async void OnStartup(StartupEventArgs e) | |
{ | |
base.OnStartup(e); | |
_mainTokenSource = new CancellationTokenSource(); | |
_mainScheduler = TaskScheduler.FromCurrentSynchronizationContext(); | |
// Real entry point for full async app :) | |
await MainFactory().StartNew(() => | |
{ | |
}); | |
} | |
protected override void OnExit(ExitEventArgs e) | |
{ | |
if(!_mainTokenSource.IsCancellationRequested) | |
_mainTokenSource.Cancel(); | |
base.OnExit(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment