title: Call method in separate thread
body: <p>I need to call class method in separate thread.  The method has a parameter and a return value.</p>
  
  <p><strong>Details</strong></p>
  
  <p>I have a Form with two TextBoxes.  User put value in the first TextBox and get the result in the second one:</p>
  
  <pre><code> private void tbWord_TextChanged(object sender, TextChangedEventArgs e)
   {
       tbResponce.Text = Wiki.DoWork(tbWord.Text);
   }
  </code></pre>
  
  <p>The Wiki-class must use Wikipedia API:</p>
  
  <pre><code>public class Wiki
  {
      private class State
      {
          public EventWaitHandle eventWaitHandle = new ManualResetEvent(false);
          public String result;
          public String word;
      }
  
      private static void PerformUserWorkItem( Object stateObject )
      {
          State state = stateObject as State;
  
          if(state != null)
          {
              Uri address = new Uri("http://en.wikipedia.org/w/api.php?action=opensearch&amp;search=" + state.word);
              HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
              HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
  
              StreamReader myStreamReader = new StreamReader(responce.GetResponseStream(), Encoding.GetEncoding(1251));
              state.result = myStreamReader.ReadToEnd();
  
              state.eventWaitHandle.Set(); 
          }
      }
  
      public static String DoWork(String _word)
      {
          State state = new State();
          state.word = _word;
  
          ThreadPool.QueueUserWorkItem(PerformUserWorkItem, state);
          state.eventWaitHandle.WaitOne();
          return state.result;                   
      }     
  }
  </code></pre>
  
  <p><em>Problem</em></p>
  
  <p>When user press keys in the tbWord the main form freeze and wait while Wiki class do all the work.</p>
  
  <p>How can I run DoWork asynchronously?</p>