using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
// To
report progress from the background worker we need to set this property
backgroundWorker1.WorkerReportsProgress = true;
// This
event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
// This
event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged
+= new
ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
private
void Form1_Load(object
sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
// On worker
thread so do our thing!
void
backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your
background task goes here, 150 Task
for
(int i = 1; i <= 150; i++)
{
decimal
percentage = (Convert.ToDecimal(i) / Convert.ToDecimal(137)) *
Convert.ToDecimal(100);
int
ProgressValue = Convert.ToInt32(percentage);
//
Report progress to 'UI' thread
backgroundWorker1.ReportProgress(ProgressValue);
//
Simulate long task
System.Threading.Thread.Sleep(100);
}
}
// Back on
the 'UI' thread so we can update the progress bar
void
backgroundWorker1_ProgressChanged(object
sender, ProgressChangedEventArgs e)
{
// The
progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
}
}
No comments:
Post a Comment