Skip to content

Instantly share code, notes, and snippets.

@thenameless314159
Created June 25, 2017 21:50
Show Gist options
  • Save thenameless314159/fb38a1aefe16da103e99374f61ca1994 to your computer and use it in GitHub Desktop.
Save thenameless314159/fb38a1aefe16da103e99374f61ca1994 to your computer and use it in GitHub Desktop.
experimental async entry point for wpf app
/// <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