Search results for 'CallBack'. 1 post(s) found.

  1. 2007/08/27 Calling Your Main Thread From A Worker Thread In C#
2007/08/27 08:37

Calling Your Main Thread From A Worker Thread In C#


There are many cases where it is necessary to let your main application know that a Thread has completed. Your main application could spawn some Threads to do background work and then when the Threads have completed their work they could return the results back to the main application. This would be beneficial since it would leave the main Thread free and ready to respond to the user. We will create a new application that will spawn two Thread that will each modify a label on a form, when the classes are finished executing they will call back to the main Thread to let it know that they have completed execution.

using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsApplication1
{       
   public class Counter
   {       
      private Label LabeltoUpdate;
      private int MaxCount;
      private CallBack CallBackMethod;
       
      public Counter(Label l,int max, CallBack cb)
      {
          LabeltoUpdate = l;
          MaxCount = max;
          CallBackMethod = cb;
      }

      public void BeginProcessing()
      {
          int i;

          for(i=1;i < MaxCount; i  )
          {
             LabeltoUpdate.Text = i.ToString();
             LabeltoUpdate.Refresh();
          }

          //Tell the main Thread we have completed
          CallBackMethod(i,Thread.CurrentThread.Name);
      }
   }
}

using System.Threading;

private void button1_Click(object sender, System.EventArgs e)
{
   Counter c1 = new Counter(label1,3000,new CallBack(ThreadCompleted));
   Counter c2 = new Counter(label2,1500,new CallBack(ThreadCompleted));

   Thread t1 = new Thread(new ThreadStart(c1.BeginProcessing));
   Thread t2 = new Thread(new ThreadStart(c2.BeginProcessing));

   t1.Name = "Counter Thread 1";
   t2.Name = "Counter Thread 2";

   t1.Start();
   t2.Start();
}

private void ThreadCompleted(int intTotal,string strName)
{
   MessageBox.Show("Thread "   strName  
                      " has completed at "   intTotal.ToString());
}

Trackback 6 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Liquid codeine how to make.

    Tracked from Extracting codeine from promethazine. 2009/05/30 08:30 delete

    Metabolism of codeine in urine. Codeine and no prescriptions and no consults. Codeine 3. Codeine can ada. What is codeine.

  2. Subject Cheapest tramadol.

    Tracked from Tramadol. 2009/06/01 19:59 delete

    Tramadol cod. Tramadol. Tramadol 50mg.

  3. Subject Tramadol.

    Tracked from Tramadol soma zoloft prozac onlineprescription.md. 2009/07/01 17:12 delete

    Tramadol.

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 05:04 delete

    moneyideas

  5. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:51 delete

    moneyideas

  6. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas