title: how can I run my Tasks on a background thread? | c# | SSH
body: <p>I am trying to run a series of tasks in the background of my Telnet / SSH program. sadly it seems I am not able to find out how to get this to work on the background.</p>
  
  <p>I have tried using <strong>Tasks</strong> : </p>
  
  <pre><code>Task ReindexStock = new Task(delegate
          {
              this.Invoke(new Action(() =&gt;
              {
                  btnReindexStock.PerformClick();
                  txtBoxInput.Text = command[1];
                  ExecuteCommand();
              }
              ));
          });
          ReindexStock.Start();
          ReindexStock.Wait();
  
          Task Product_attribute = new Task(delegate
          {
              this.Invoke(new Action(() =&gt;
              {
                  btnReindexProduct_Attribute.PerformClick();
                  txtBoxInput.Text = command[2];
                  ExecuteCommand();
              }
            ));
          });
          Product_attribute.Start();
          Product_attribute.Wait();
  </code></pre>
  
  <p>I also tried <strong>threads</strong> :</p>
  
  <pre><code>new Thread(() =&gt;
      {
          btnReindexStock.PerformClick();
          txtBoxInput.Text = command[1];
          ExecuteCommand();
      }).Start();
  new Thread(() =&gt;
      {
          btnReindexProduct_Attribute.PerformClick();
          txtBoxInput.Text = command[2];
          ExecuteCommand();
      }).Start();
  </code></pre>
  
  <p>as well as <strong>this</strong> ( plucked this one from the net, hoped it would work ):</p>
  
  <pre><code>          ThreadPool.QueueUserWorkItem(delegate
        {
            btnReindexStock.PerformClick();
            txtBoxInput.Text = command[1];
            ExecuteCommand();
        });
        ThreadPool.QueueUserWorkItem(delegate
        {
            btnReindexProduct_Attribute.PerformClick();
            txtBoxInput.Text = command[2];
            ExecuteCommand();
        });
  </code></pre>
  
  <p>But for some reason my program still freezes when it's doing ExecuteCommand (</p>
  
  <pre><code>  var cmd = SSH.client.CreateCommand(txtBoxInput.Text);
          var result = cmd.Execute();
          this.Invoke(new Action(() =&gt;
          {
              rTxtBoxOutput.Text += result;
  
              var reader = new StreamReader(cmd.ExtendedOutputStream);
              rTxtBoxOutput.Text += "\n" + reader.ReadToEnd();
          }
          ));
  </code></pre>
  
  <p>)</p>
  
  <p>I have tried several things in the backgroundworker_doWork aswell but none of them seemed to work.. I tried to start a new thread like this </p>
  
  <pre><code>new Thread(new ThreadStart(ReindexAll)).Start();
  </code></pre>
  
  <p>also like this but I guess this is practically the same but larger</p>
  
  <pre><code>Thread t = new Thread(new ThreadStart(ReindexAll));
              t.Start();
              t.IsBackground = true;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Task.Factory.StartNew(() =&gt; ReindexAll());
  </code></pre>
  
  <p>as well as the plain and simple </p>
  
  <pre><code>ReindexAll();
  </code></pre>
  
  <p>but as I said before none of it seems to work, the moment I execute my command the program freezes.</p>
  
  <p>Now my question is if someone is able to tell me what I am doing wrong and hopefully help me</p>