Threads

Display the main thread for the application

 
private void frmMain_Load(object sender, System.EventArgs e)
{
lblThreadID.Text += AppDomain.GetCurrentThreadId().ToString();
}

private void cmdSameThread_Click(object sender, System.EventArgs e)
{
//Run the task on the same thread that is managing the frmMain window.
TheLongRunningTask();
}


To run the task on a worker pool thread, you can use an asynchronous
invocation on a delegate. For this example, we'll declare a delegate
named TaskDelegate, and call it asynchronously. The signature of the
delegate declaration must match the method (TheLongRunningTask) exactly.


delegate void TaskDelegate();

private void cmdWorkerPoolThread_Click(object sender, System.EventArgs e)
{
//To run the task an a thread from the worker pool, create an instance
//of a delegate whose signature matches the method that will be called,
//then call BeginInvoke on that delegate. For this example, the arguments
//and return value of BeginInvoke are unneeded. This technique is sometimes
//referred to as "Fire and Forget".

TaskDelegate td = new TaskDelegate(new ThreadStart(TheLongRunningTask));
td.BeginInvoke(null, null); // Runs on a worker thread from the pool
}

private void cmdRunOnNewWin32Thread_Click(object sender, System.EventArgs e)
{
//To run the task on a newly created OS thread (rather than a tread from the
//thread pool), create a new instance of a managed thread. The constructor
//takes the address of a subroutine with no arguments.
Thread t = new Thread(new ThreadStart(TheLongRunningTask)); // Creates the new thread
t.Start(); // Begins the execution of the new thread
}


This method simulates some long-running task. To represent the work in
progress, a form with a progress bar will display during its execution.
The method displays the form, then updates the progress bar every half
second for 5 seconds. After simulating the task, the form is taken down.

private void TheLongRunningTask()
{

frmTaskProgress f = new frmTaskProgress();

f.Show();
f.Refresh();// Refresh causes an instant (non-posted) display of the label.

//Slowly increment the progress bar
int i;

for(i = 1; i < 10; i++)
{
f.prgTaskProgress.Value += 10;
Thread.Sleep(500); // half-second delay
}

//Remove the form after the "task" finishes
f.Hide();
f.Dispose();
}


Comments

Author: aradhya29 Apr 2009 Member Level: Silver   Points : 1

good program, any one if saw it first time can understand easily very easily explained.

Author: Pradeep Iyer29 Apr 2009 Member Level: Gold   Points : 1

Hi,

Good one...

Explained nicely..

Thank you

Regards,
Pradeep Y

Author: Deepika Haridas13 Jun 2009 Member Level: Gold   Points : 1

Hi,

Thanks for your valuable responses..

Keep contributing..

Regards,
Deepika



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: