title: I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?
body: <p>I have a watcher method i'm calling from the constructor of fomr1:</p>
  
  <pre><code>FileSystemWatcher watcher;
          private void WatchDirectory()
          {
              watcher = new FileSystemWatcher();
              watcher.Path = userVideosDirectory;
              watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
              watcher.Filter = "*.mp4";
              watcher.Changed += new FileSystemEventHandler(OnChanged);
              watcher.EnableRaisingEvents = true;
          } 
  </code></pre>
  
  <p>Then the Onchanged event:</p>
  
  <pre><code>private void OnChanged(object source, FileSystemEventArgs e)
          {
  
                  var info = new FileInfo(e.FullPath);
                  fileforupload = info.FullName;
                  if (IsFileLocked(info) == false)
                  {
                      sy.SetResult(true);
                      watcher.EnableRaisingEvents = false;
                      watcher.Dispose();
                  }
          }
  </code></pre>
  
  <p>And this is the IsFileLocked method:</p>
  
  <pre><code>protected virtual bool IsFileLocked(FileInfo file)
          {
              FileStream stream = null;
              try
              {
                  stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
              }
              catch (IOException)
              {
                  return true;
              }
              finally
              {
                  if (stream != null)
                      stream.Close();
              }
  </code></pre>
  
  <p>And i'm using it in this method:</p>
  
  <pre><code>public string SendResponse(HttpListenerRequest request)
          {            
                      sy = new TaskCompletionSource&lt;bool&gt;();
                      WatchDirectory();
                      sy.Task.Wait();
                      Youtube_Uploader youtubeupload = new Youtube_Uploader(fileforupload);
                      return false;
          }
  </code></pre>
  
  <p>The exception is in the OnChanged event on the line:</p>
  
  <pre><code>sy.SetResult(true);
  </code></pre>
  
  <p>sy is:</p>
  
  <pre><code>TaskCompletionSource&lt;bool&gt; sy;
  </code></pre>
  
  <p>In the OnChanged method i did:</p>
  
  <pre><code>watcher.EnableRaisingEvents = false;
  watcher.Dispose();
  </code></pre>
  
  <p>But still getting sometimes the exception.
  The exception is:</p>
  
  <pre><code>An attempt was made to transition a task to a final state when it had already completed
  
  System.InvalidOperationException was unhandled
    _HResult=-2146233079
    _message=An attempt was made to transition a task to a final state when it had already completed.
    HResult=-2146233079
    IsTransient=false
    Message=An attempt was made to transition a task to a final state when it had already completed.
    Source=mscorlib
    StackTrace:
         at System.Threading.Tasks.TaskCompletionSource`1.SetResult(TResult result)
         at Automatic_Record.Form1.OnChanged(Object source, FileSystemEventArgs e) in d:\C-Sharp\Automatic_Record\Automatic_Record\Automatic_Record\Form1.cs:line 197
         at System.IO.FileSystemWatcher.OnChanged(FileSystemEventArgs e)
         at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
         at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
         at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
    InnerException: 
  </code></pre>