title: Keep UI thread responsive when running long task in windows forms
body: <p>I am trying to read a large text file into a TextBox and keep the ui responsive when a file is dragged to the textbox.</p>
  
  <p>Not works as expected, the windows forms is frozen and only seems working the task reading the file and appending the content to the textbox.</p>
  
  <p>A ContextSwitchDeadLock was thrown by the IDE, but not really an error.
  This is a long running task. I have fixed it changing the behaviour under the exception menu.</p>
  
  <p>Thanks to JSteward, Peter changed the code to this.</p>
  
  <p>How I can keep the ui ( main thread) responsive when running this task ?
  Thanks.</p>
  
  <pre><code>private SynchronizationContext fcontext;
  
  public Form1()
  {      
      InitializeComponent();            
      values.DragDrop += values_DragDrop; //&lt;----------- This is a textbox
      fcontext = WindowsFormsSynchronizationContext.Current;
  }
  
  // The async callback 
  async void values_DragDrop(object sender, DragEventArgs e)
  {
      try
      {
          string dropped = ( (string[]) e.Data.GetData(DataFormats.FileDrop))[0];
          if ( dropped.Contains(".csv") || dropped.Contains(".txt"))
          {
                  using ( StreamReader sr = File.OpenText(dropped) )
                  {
                      string s = String.Empty;
                      while ( ( s = await sr.ReadLineAsync() ) != null )
                      {                                                                
                         values.AppendText(s.Replace(";",""));
                      }
                  }                 
           }
       }
    catch (Exception ex) { }
  }
  </code></pre>