Introducing the Task-Based Asynchronous Pattern: Listing 3.

The TaskCompletionSource<T> Object

static public Task<Process> RunProcessAsync(string fileName,
  CancellationToken cancellationToken)
{
  TaskCompletionSource<Process> taskCompletionSource =
    new TaskCompletionSource<Process>(); 
  Process process = new Process()
  {
    StartInfo = new ProcessStartInfo(fileName)
    {
      UseShellExecute = false
    },
    EnableRaisingEvents = true,
  };
 
  process.Exited += (sender, localEventArgs) =>
  {
    taskCompletionSource.SetResult(process);
  };
 
  if (cancellationToken.IsCancellationRequested)
  {
    cancellationToken.ThrowIfCancellationRequested();
  }
 
  process.Start();
 
  if (cancellationToken.Register( ()=>
    {
      process.CloseMainWindow();
      cancellationToken.ThrowIfCancellationRequested();
    }
 
  return taskCompletionSource.Task;
}

About the Author

Mark Michaelis (http://IntelliTect.com/Mark) is the founder of IntelliTect and serves as the Chief Technical Architect and Trainer. Since 1996, he has been a Microsoft MVP for C#, Visual Studio Team System, and the Windows SDK and in 2007 he was recognized as a Microsoft Regional Director. He also serves on several Microsoft software design review teams, including C#, the Connected Systems Division, and VSTS. Mark speaks at developer conferences and has written numerous articles and books - Essential C# 5.0 is his most recent. Mark holds a Bachelor of Arts in Philosophy from the University of Illinois and a Masters in Computer Science from the Illinois Institute of Technology. When not bonding with his computer, Mark is busy with his family or training for another triathlon (having completed the Ironman in 2008). Mark lives in Spokane, Washington, with his wife Elisabeth and three children, Benjamin, Hanna and Abigail.

comments powered by Disqus

Featured

  • Xamarin.Forms 5 Preview Ships Ahead of .NET 6 Transition to MAUI

    Microsoft shipped a pre-release version of Xamarin.Forms 5 ahead of a planned transition to MAUI, which will take over beginning with the release of .NET 6 in November 2021.

  • ML.NET Improves Object Detection

    Microsoft improved the object detection capabilities of its ML.NET machine learning framework for .NET developers, adding the ability to train custom models with Model Builder in Visual Studio.

  • More Improvements for VS Code's New Python Language Server

    Microsoft announced more improvements for the new Python language server for Visual Studio Code, Pylance, specializing in rich type information.

  • Death of the Dev Machine?

    Here's a takeaway from this week's Ignite 2020 event: An advanced Azure cloud portends the death of the traditional, high-powered dev machine packed with computing, memory and storage components.

  • COVID-19 Is Ignite 2020's Elephant in the Room: 'Frankly, It Sucks'

    As in all things of our new reality, there was no escaping the drastic changes in routine caused by the COVID-19 pandemic during Microsoft's big Ignite 2020 developer/IT pro conference, this week shifted to an online-only event after drawing tens of thousands of in-person attendees in years past.

Upcoming Events