VS2005 new control for windows programming :BackgroundWorker
2008-05-02 14:02
393 查看
VS2005新增控件介绍:BackgroundWorker
BackgroundWorker组件:
1、使用BackgroundWorker组件时,您可以在不同于应用程序的主用户界面线程的另一线程上异步(“在后台”)执行耗时的 操作。
2、若要使用BackgroundWorker,只需要告诉该组件要在后台执行的耗时的辅助方法,然后调用RunWorkerAsync方法。
3、在辅助方法以异步方式运行的同时,您的调用线程继续正常运行。该方法运行完毕,BackgroundWorker激发RunWorkerCompleted事件(看选择包含操作结果)向调用线程发出警报。
BackgroundWorker示例:后台下载文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UseBackgroundWorker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private XmlDocument document = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void dowloadButton_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.backgroundWorker1.RunWorkerAsync();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Disable the button for the duration of the download.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.dowloadButton.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Wait for the BackgroundWorker to finish the download.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (this.backgroundWorker1.IsBusy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Keep UI messages moving, so the form remains
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// responsive during the asynchronous operation.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.DoEvents();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// The download is done, so enable the button.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.dowloadButton.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document = new XmlDocument();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Replace this file name with a valid file name.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.Load(@"http://www.tailspintoys.com/sample.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Uncomment the following line to
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// simulate a noticeable latency.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Error == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(document.InnerXml, "Download Complete");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Failed to download file",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Download failed",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBoxButtons.OK,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBoxIcon.Error);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
BackgroundWorker组件:
1、使用BackgroundWorker组件时,您可以在不同于应用程序的主用户界面线程的另一线程上异步(“在后台”)执行耗时的 操作。
2、若要使用BackgroundWorker,只需要告诉该组件要在后台执行的耗时的辅助方法,然后调用RunWorkerAsync方法。
3、在辅助方法以异步方式运行的同时,您的调用线程继续正常运行。该方法运行完毕,BackgroundWorker激发RunWorkerCompleted事件(看选择包含操作结果)向调用线程发出警报。
BackgroundWorker示例:后台下载文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UseBackgroundWorker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public partial class Form1 : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private XmlDocument document = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Form1()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void dowloadButton_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.backgroundWorker1.RunWorkerAsync();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Disable the button for the duration of the download.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.dowloadButton.Enabled = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Wait for the BackgroundWorker to finish the download.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (this.backgroundWorker1.IsBusy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Keep UI messages moving, so the form remains
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// responsive during the asynchronous operation.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.DoEvents();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// The download is done, so enable the button.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.dowloadButton.Enabled = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document = new XmlDocument();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Replace this file name with a valid file name.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.Load(@"http://www.tailspintoys.com/sample.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Uncomment the following line to
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// simulate a noticeable latency.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Error == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(document.InnerXml, "Download Complete");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Failed to download file",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Download failed",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBoxButtons.OK,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBoxIcon.Error);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- VS2005 new control for windows programming :MaskedTextBox
- Windows Vista for Developers – A New Series
- 《Premier.Press.Microsoft.Windows.Shell.Script.Programming.for.the.Absolute.Beginner》
- Designing and Implement ButtonEdit Control for Windows Forms
- Designing and Implement ButtonEdit Control for Windows Forms
- Network Programming for Microsoft Windows
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
- Network Programming for Microsoft Windows 2nd(Windows 网络编程第二版) 学习心得
- Lockless Programming Considerations for Xbox 360 and Microsoft Windows
- [转]A new OpenAL 1.1 Installer for Windows and a new OpenAL 1.1 SDK for Windows are now available
- A New Skin for Windows Mobile?
- VS2005 build for Windows Mobile 6 SDK Samples
- Network Programming for Microsoft Windows 2nd Edition
- Programming Applications for Microsoft Windows(二)
- a new Webcam Api Tutorial in C++ for Windows(Windows Media Foundation)--WMF
- Designing and Implement Lookup Control for Windows Forms
- Designing and Implement Lookup Control for Windows Forms
- Designing and Implement ButtonEdit Control for Windows Forms
- NEWT (Network Emulator for Windows Toolkit)
- Designing and Implement Lookup Control for Windows Forms