title: Timeout for async task
body: <p>My application currently executes Adobe Illustrator with some command. Waits when result file appears in some exact folder (with async function) and does something with file when it's ready. </p>
  
  <p>But the problem is, sometimes Adobe Illustrator fails and app keeps waiting. In such cases I can't figure out, how can I apply timeout mechanism to kill Adobe Illustrator and skip current process.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>...
  await WhenFileCreated(result_file_name);
  if (File.Exists(result_file_name))
  {
  ...
  
  
  public static Task WhenFileCreated(string path)
  {
      if (File.Exists(path))
          return Task.FromResult(true);
  
      var tcs = new TaskCompletionSource&lt;bool&gt;();
      FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(path));
  
      FileSystemEventHandler createdHandler = null;
      RenamedEventHandler renamedHandler = null;
      createdHandler = (s, e) =&gt;
      {
          if (e.Name == Path.GetFileName(path))
          {
              tcs.TrySetResult(true);
              watcher.Created -= createdHandler;
              watcher.Dispose();
          }
      };
  
      renamedHandler = (s, e) =&gt;
      {
          if (e.Name == Path.GetFileName(path))
          {
              tcs.TrySetResult(true);
              watcher.Renamed -= renamedHandler;
              watcher.Dispose();
          }
      };
  
      watcher.Created += createdHandler;
      watcher.Renamed += renamedHandler;
  
      watcher.EnableRaisingEvents = true;
  
      return tcs.Task;
  }
  </code></pre>
  
  <p>How to apply timeout to this? Any suggestions?</p>