110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace BackgroundWorkerExample
{
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
int arg = (int)e.Argument;
e.Result = CalculateSum(bw, arg);
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripProgressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
this.toolStripStatusLabel1.Text = "Operation was canceled";
}
else if (e.Error != null)
{
string msg = String.Format("An error occurred: {0}", e.Error.Message);
this.toolStripStatusLabel1.Text = msg;
}
else
{
txtResult.Text = e.Result.ToString();
this.toolStripStatusLabel1.Text = "Finished successfully.";
}
this.toolStripProgressBar1.Value = 0;
}
private int CalculateSum(BackgroundWorker bw, int maxNum)
{
int result = 0;
for (int i = 0; i < maxNum; i++)
{
if (bw.CancellationPending) break;
result += i;
bw.ReportProgress((i * 100) / maxNum);
Thread.Sleep(500);
}
return result;
}
private void startBtn_Click(object sender, EventArgs e)
{
int number = 0;
if (int.TryParse(txtNumber.Text, out number))
{
this.toolStripStatusLabel1.Text = "Start calculating ...";
this.backgroundWorker1.RunWorkerAsync(number);
}
else
MessageBox.Show("Please input max number before calculating");
}
private void cancelBtn_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
}
private TextBox txtNumber;
private Label lblNumber;
private Label lblResult;
private TextBox txtResult;
private StatusStrip statusStrip1;
private ToolStripStatusLabel toolStripStatusLabel1;
private ToolStripProgressBar toolStripProgressBar1;
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
startBtn = new Button();
cancelBtn = new Button();
txtNumber = new TextBox();
lblNumber = new Label();
lblResult = new Label();
txtResult = new TextBox();
statusStrip1 = new StatusStrip();
toolStripStatusLabel1 = new ToolStripStatusLabel();
toolStripProgressBar1 = new ToolStripProgressBar();
statusStrip1.SuspendLayout();
SuspendLayout();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
startBtn.Location = new Point(61, 75);
startBtn.Name = "startBtn";
startBtn.Size = new Size(75, 23);
startBtn.TabIndex = 0;
startBtn.Text = "Start";
startBtn.Click += new EventHandler(this.startBtn_Click);
cancelBtn.Location = new Point(161, 75);
cancelBtn.Name = "cancelBtn";
cancelBtn.Size = new Size(75, 23);
cancelBtn.TabIndex = 1;
cancelBtn.Text = "Cancel";
cancelBtn.Click += new EventHandler(this.cancelBtn_Click);
txtNumber.Location = new Point(113, 12);
txtNumber.Name = "txtNumber";
txtNumber.Size = new Size(148, 20);
txtNumber.TabIndex = 2;
lblNumber.AutoSize = true;
lblNumber.Location = new Point(40, 12);
lblNumber.Name = "lblNumber";
lblNumber.Size = new Size(67, 13);
lblNumber.TabIndex = 3;
lblNumber.Text = "Max Number";
lblResult.AutoSize = true;
lblResult.Location = new Point(40, 43);
lblResult.Name = "lblResult";
lblResult.Size = new Size(37, 13);
lblResult.TabIndex = 3;
lblResult.Text = "Result";
txtResult.Location = new Point(113, 40);
txtResult.Name = "txtResult";
txtResult.ReadOnly = true;
txtResult.Size = new Size(148, 20);
txtResult.TabIndex = 2;
statusStrip1.Items.AddRange(new ToolStripItem[] {
toolStripStatusLabel1,
toolStripProgressBar1});
statusStrip1.Location = new Point(0, 121);
statusStrip1.Name = "statusStrip1";
statusStrip1.Size = new Size(296, 22);
statusStrip1.TabIndex = 4;
statusStrip1.Text = "statusStrip1";
toolStripStatusLabel1.Name = "toolStripStatusLabel1";
toolStripStatusLabel1.Size = new Size(179, 17);
toolStripStatusLabel1.Spring = true;
toolStripStatusLabel1.Text = "Status";
toolStripStatusLabel1.TextAlign = ContentAlignment.MiddleLeft;
toolStripProgressBar1.Name = "toolStripProgressBar1";
toolStripProgressBar1.Size = new Size(100, 16);
toolStripProgressBar1.Step = 1;
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(296, 143);
Controls.Add(this.statusStrip1);
Controls.Add(this.lblResult);
Controls.Add(this.lblNumber);
Controls.Add(this.txtResult);
Controls.Add(this.txtNumber);
Controls.Add(this.cancelBtn);
Controls.Add(this.startBtn);
FormBorderStyle = FormBorderStyle.FixedDialog;
Name = "Form1";
Text = "Calucate sum";
statusStrip1.ResumeLayout(false);
statusStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
private BackgroundWorker backgroundWorker1;
private Button startBtn;
private Button cancelBtn;
}
public class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
WM_USER = 0x0400
SBM_SETRANGE = 0x00E;
PBM_SETPOS = (WM_USER + 2);
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll",EntryPoint="FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("User32.dll",EntryPoint="PostMessage")]
private static extern int PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
PostMessage(hWnd,SBM_SETRANGE,0,100); //设置进度条最小最大值
PostMessage(hWnd,PBM_SETPOS,25, 0);//设置进度。