c#中关于backgroundWorker控件的用法
2010-12-30 10:29
387 查看
所需控件如下: 三个textbox,两个button,一个进度条,一个backgroundWorker控件
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//设置计算按扭与取消按扭的使用属性以及进度条的进度值
this.button1.Enabled = false;
this.textBox3.Text = string.Empty;
this.button2.Enabled = true;
this.progressBar1.Value = 0;
backgroundWorker1.RunWorkerAsync(new CalcInput(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)));//开始执行后台操作
}
//当执行后台操作时,后台线程开始执行此操作
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CalcInput input = (CalcInput)e.Argument;
//System.Threading.Thread.Sleep(5000);
//e.Result = input.x + input.y;
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
backgroundWorker1.ReportProgress(i * 10);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
e.Result = input.x + input.y;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage; //设置进度条的value值
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.textBox3.Text = e.Result.ToString();
//this.button2.Enabled = true;
//this.button1.Enabled = false;
//this.progressBar1.Value = 100;
if (e.Cancelled)
{
this.textBox3.Text = "cancelled";
}
else
{
this.textBox3.Text = e.Result.ToString();
}
this.button2.Enabled = true;
this.button1.Enabled = false;
this.progressBar1.Value = 100;
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();//执行此操作时,需将backgroundWorker1控件WorkerSupportCancellation属性设置为true
}
}
//记录两个文本框的值
public struct CalcInput
{
public CalcInput(int x, int Y)
{
this.x = x;
this.y = Y;
}
public int x;
public int y;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//设置计算按扭与取消按扭的使用属性以及进度条的进度值
this.button1.Enabled = false;
this.textBox3.Text = string.Empty;
this.button2.Enabled = true;
this.progressBar1.Value = 0;
backgroundWorker1.RunWorkerAsync(new CalcInput(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)));//开始执行后台操作
}
//当执行后台操作时,后台线程开始执行此操作
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CalcInput input = (CalcInput)e.Argument;
//System.Threading.Thread.Sleep(5000);
//e.Result = input.x + input.y;
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
backgroundWorker1.ReportProgress(i * 10);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
}
e.Result = input.x + input.y;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage; //设置进度条的value值
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.textBox3.Text = e.Result.ToString();
//this.button2.Enabled = true;
//this.button1.Enabled = false;
//this.progressBar1.Value = 100;
if (e.Cancelled)
{
this.textBox3.Text = "cancelled";
}
else
{
this.textBox3.Text = e.Result.ToString();
}
this.button2.Enabled = true;
this.button1.Enabled = false;
this.progressBar1.Value = 100;
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();//执行此操作时,需将backgroundWorker1控件WorkerSupportCancellation属性设置为true
}
}
//记录两个文本框的值
public struct CalcInput
{
public CalcInput(int x, int Y)
{
this.x = x;
this.y = Y;
}
public int x;
public int y;
}
相关文章推荐
- c#中关于backgroundWorker控件的用法 (转)
- C#WinFrom开发系列之关于动态添加生成和删除控件的相关知识
- C#中关于using的用法
- 关于GRidView控件的用法总结
- c# 保留小数----关于System.Math.Round()的用法
- C# 2.0 中关于泛型的用法实例
- C# SplitContainer 控件详细用法
- 关于C#中的new的用法
- 关于c# 泛型List集合对象的用法及相关操作
- c#:RadioButton控件与CheckBox控件的用法
- C#更换皮肤IrisSkin2控件用法
- 关于C# math的用法
- C#关于自带滚动条控件的滚动条跳动问题
- 关于UILabel的autolayout用法——子控件决定父控件的高度
- C#学习笔记:RadioButton控件与CheckBox控件的用法
- 关于c#中的Timer控件的简单用法
- C#关于控件随窗体的改变而自动调整大小并且能重新定位的一些简单介绍和控件随窗体改变自适应类
- C# winform 中LinkLable控件的用法
- C# 关于控件的z-order(Z轴次序)属性(By dlm)
- C#关于自带滚动条控件的滚动条跳动问题