|
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
#endregion
namespace WindowsApplication1
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
this.button2.Enabled = true;
this.backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
e.Result = this.LongRunningMethod(sender as BackgroundWorker, e);
}
private long LongRunningMethod(BackgroundWorker instance, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
if (instance.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(100);
instance.ReportProgress(i);
}
}
return 1L;
}
private void button2_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
this.button2.Enabled = false;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.button1.Enabled = true;
this.progressBar1.Value = 0;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Rakesh Rajan is a Software Engineer from India working at Technopark, Trivandrum in Kerala. He is a Microsoft MVP and an MCSD (.NET) with a few other certifications, and had been working in .NET for the past 3 years. He graduated majoring in Computer Science during his memorable days at Ooty (a wonderful hill station in Southern India). You can find him posting at newgroups, writing articles, working on his own projects or taking some time off by listening to music by Enya or Yanni, or reading an Archer or Sheldon.
Find his online publications
here.
Rakesh blogs at
http://rakeshrajan.com/blog/ and maintains a site
http://rakeshrajan.com/.
He used to blog at
http://www.msmvps.com/rakeshrajan/.
Drop him a mail at rakeshrajan {at} mvps {dot} org.