Search results for 'CallBack'. 1 post(s) found.
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());
}
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());
}
Another posts included in "C#"
| Creating A Watched Folder With Assigned Events (0) | 2007/08/27 |
| Interacting With TinyPic From C# (0) | 2007/08/27 |
| How to send binary data through C# CGI app? (0) | 2007/08/27 |
| HashTable Tutorial In C# (0) | 2007/08/27 |
| Stack Tutorial In C# (0) | 2007/08/27 |
| Queue Tutorial In C# (0) | 2007/08/27 |
| Create PDF Files On the Fly In C Sharp (0) | 2007/08/27 |
| How to call Visual C/C++ implemented DLL functions in C# - Simple DLLIm... (0) | 2007/08/26 |
Trackback : Cannot send a trackbact to this post.
-
Subject Liquid codeine how to make.
2009/05/30 08:30
Metabolism of codeine in urine. Codeine and no prescriptions and no consults. Codeine 3. Codeine can ada. What is codeine.
-
Subject Cheapest tramadol.
2009/06/01 19:59
Tramadol cod. Tramadol. Tramadol 50mg.
-
Subject Tramadol.
2009/07/01 17:12
Tramadol.
-
Subject different money making ideas
2010/01/29 05:04
moneyideas
-
Subject different money making ideas
2010/01/29 13:51
moneyideas
-
Subject different money making ideas
2010/01/31 16:44
moneyideas

Prev

Rss Feed