asp.net BackgroundWorker之在后台下载文件
程序员文章站
2024-03-06 11:53:49
示例: 下面的代码示例演示如何使用 backgroundworker 组件从 url 加载 xml 文件。用户单击“下载”按钮时,click 事件处理程序将调用 backg...
示例:
下面的代码示例演示如何使用 backgroundworker 组件从 url 加载 xml 文件。用户单击“下载”按钮时,click 事件处理程序将调用 backgroundworker 组件的 runworkerasync 方法来启动下载操作。在下载过程中,将禁用该按钮,然后在下载完成后再启用该按钮。messagebox 将显示文件的内容。
using system;
using system.collections.generic;
using system.componentmodel;
using system.drawing;
using system.threading;
using system.windows.forms;
using system.xml;
public class form1 : form
{
private backgroundworker backgroundworker1;
private button dowloadbutton;
private xmldocument document = null;
public form1()
{
initializecomponent();
}
private void dowloadbutton_click(object sender, eventargs e)
{
// start the download operation in the background.
this.backgroundworker1.runworkerasync();
// disable the button for the duration of the download.
this.dowloadbutton.enabled = false;
// wait for the backgroundworker to finish the download.
while (this.backgroundworker1.isbusy)
{
// keep ui messages moving, so the form remains
// responsive during the asynchronous operation.
application.doevents();
}
// the download is done, so enable the button.
this.dowloadbutton.enabled = true;
}
private void backgroundworker1_dowork(
object sender,
doworkeventargs e)
{
document = new xmldocument();
// replace this file name with a valid file name.
document.load(@"http://www.tailspintoys.com/sample.xml");
// uncomment the following line to
// simulate a noticeable latency.
//thread.sleep(5000);
}
private void backgroundworker1_runworkercompleted(
object sender,
runworkercompletedeventargs e)
{
if (e.error == null)
{
messagebox.show(document.innerxml, "download complete");
}
else
{
messagebox.show(
"failed to download file",
"download failed",
messageboxbuttons.ok,
messageboxicon.error );
}
}
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.icontainer components = null;
/// <summary>
/// clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void dispose(bool disposing)
{
if (disposing && (components != null))
{
components.dispose();
}
base.dispose(disposing);
}
#region windows form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.backgroundworker1 = new system.componentmodel.backgroundworker();
this.dowloadbutton = new system.windows.forms.button();
this.suspendlayout();
//
// backgroundworker1
//
this.backgroundworker1.dowork += new system.componentmodel.doworkeventhandler(this.backgroundworker1_dowork);
this.backgroundworker1.runworkercompleted += new system.componentmodel.runworkercompletedeventhandler(this.backgroundworker1_runworkercompleted);
//
// dowloadbutton
//
this.dowloadbutton.location = new system.drawing.point(12, 12);
this.dowloadbutton.name = "dowloadbutton";
this.dowloadbutton.size = new system.drawing.size(75, 23);
this.dowloadbutton.tabindex = 0;
this.dowloadbutton.text = "download file";
this.dowloadbutton.usevisualstylebackcolor = true;
this.dowloadbutton.click += new system.eventhandler(this.dowloadbutton_click);
//
// form1
//
this.autoscaledimensions = new system.drawing.sizef(6f, 13f);
this.autoscalemode = system.windows.forms.autoscalemode.font;
this.clientsize = new system.drawing.size(104, 54);
this.controls.add(this.dowloadbutton);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
}
static class program
{
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.enablevisualstyles();
application.run(new form1());
}
}
下载文件:
文件下载在 backgroundworker 组件的辅助线程上进行,该线程运行 dowork 事件处理程序。当代码调用 runworkerasync 方法时,将启动此线程。
private void backgroundworker1_dowork(
object sender,
doworkeventargs e)
{
document = new xmldocument();
// replace this file name with a valid file name.
document.load(@"http://www.tailspintoys.com/sample.xml");
// uncomment the following line to
// simulate a noticeable latency.
//thread.sleep(5000);
}
等待 backgroundworker 完成
dowloadbutton_click 事件处理程序演示如何等待 backgroundworker 组件完成它的异步任务。使用 isbusy 属性可以确定 backgroundworker 线程是否仍在运行。如果代码在主 ui 线程上(对于 click 事件处理程序即是如此),请务必调用 application.doevents 方法以使用户界面能够响应用户操作。
private void dowloadbutton_click(object sender, eventargs e)
{
// start the download operation in the background.
this.backgroundworker1.runworkerasync();
// disable the button for the duration of the download.
this.dowloadbutton.enabled = false;
// wait for the backgroundworker to finish the download.
while (this.backgroundworker1.isbusy)
{
// keep ui messages moving, so the form remains
// responsive during the asynchronous operation.
application.doevents();
}
// the download is done, so enable the button.
this.dowloadbutton.enabled = true;
}
显示结果
backgroundworker1_runworkercompleted 方法将处理 runworkercompleted 事件,并在后台操作完成后被调用。它首先检查 asynccompletedeventargs.error 属性,如果该属性是 null,它将显示文件内容。
private void backgroundworker1_runworkercompleted(
object sender,
runworkercompletedeventargs e)
{
if (e.error == null)
{
messagebox.show(document.innerxml, "download complete");
}
else
{
messagebox.show(
"failed to download file",
"download failed",
messageboxbuttons.ok,
messageboxicon.error );
}
}
下面的代码示例演示如何使用 backgroundworker 组件从 url 加载 xml 文件。用户单击“下载”按钮时,click 事件处理程序将调用 backgroundworker 组件的 runworkerasync 方法来启动下载操作。在下载过程中,将禁用该按钮,然后在下载完成后再启用该按钮。messagebox 将显示文件的内容。
复制代码 代码如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.drawing;
using system.threading;
using system.windows.forms;
using system.xml;
public class form1 : form
{
private backgroundworker backgroundworker1;
private button dowloadbutton;
private xmldocument document = null;
public form1()
{
initializecomponent();
}
private void dowloadbutton_click(object sender, eventargs e)
{
// start the download operation in the background.
this.backgroundworker1.runworkerasync();
// disable the button for the duration of the download.
this.dowloadbutton.enabled = false;
// wait for the backgroundworker to finish the download.
while (this.backgroundworker1.isbusy)
{
// keep ui messages moving, so the form remains
// responsive during the asynchronous operation.
application.doevents();
}
// the download is done, so enable the button.
this.dowloadbutton.enabled = true;
}
private void backgroundworker1_dowork(
object sender,
doworkeventargs e)
{
document = new xmldocument();
// replace this file name with a valid file name.
document.load(@"http://www.tailspintoys.com/sample.xml");
// uncomment the following line to
// simulate a noticeable latency.
//thread.sleep(5000);
}
private void backgroundworker1_runworkercompleted(
object sender,
runworkercompletedeventargs e)
{
if (e.error == null)
{
messagebox.show(document.innerxml, "download complete");
}
else
{
messagebox.show(
"failed to download file",
"download failed",
messageboxbuttons.ok,
messageboxicon.error );
}
}
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.icontainer components = null;
/// <summary>
/// clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void dispose(bool disposing)
{
if (disposing && (components != null))
{
components.dispose();
}
base.dispose(disposing);
}
#region windows form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.backgroundworker1 = new system.componentmodel.backgroundworker();
this.dowloadbutton = new system.windows.forms.button();
this.suspendlayout();
//
// backgroundworker1
//
this.backgroundworker1.dowork += new system.componentmodel.doworkeventhandler(this.backgroundworker1_dowork);
this.backgroundworker1.runworkercompleted += new system.componentmodel.runworkercompletedeventhandler(this.backgroundworker1_runworkercompleted);
//
// dowloadbutton
//
this.dowloadbutton.location = new system.drawing.point(12, 12);
this.dowloadbutton.name = "dowloadbutton";
this.dowloadbutton.size = new system.drawing.size(75, 23);
this.dowloadbutton.tabindex = 0;
this.dowloadbutton.text = "download file";
this.dowloadbutton.usevisualstylebackcolor = true;
this.dowloadbutton.click += new system.eventhandler(this.dowloadbutton_click);
//
// form1
//
this.autoscaledimensions = new system.drawing.sizef(6f, 13f);
this.autoscalemode = system.windows.forms.autoscalemode.font;
this.clientsize = new system.drawing.size(104, 54);
this.controls.add(this.dowloadbutton);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
}
static class program
{
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.enablevisualstyles();
application.run(new form1());
}
}
下载文件:
文件下载在 backgroundworker 组件的辅助线程上进行,该线程运行 dowork 事件处理程序。当代码调用 runworkerasync 方法时,将启动此线程。
复制代码 代码如下:
private void backgroundworker1_dowork(
object sender,
doworkeventargs e)
{
document = new xmldocument();
// replace this file name with a valid file name.
document.load(@"http://www.tailspintoys.com/sample.xml");
// uncomment the following line to
// simulate a noticeable latency.
//thread.sleep(5000);
}
等待 backgroundworker 完成
dowloadbutton_click 事件处理程序演示如何等待 backgroundworker 组件完成它的异步任务。使用 isbusy 属性可以确定 backgroundworker 线程是否仍在运行。如果代码在主 ui 线程上(对于 click 事件处理程序即是如此),请务必调用 application.doevents 方法以使用户界面能够响应用户操作。
复制代码 代码如下:
private void dowloadbutton_click(object sender, eventargs e)
{
// start the download operation in the background.
this.backgroundworker1.runworkerasync();
// disable the button for the duration of the download.
this.dowloadbutton.enabled = false;
// wait for the backgroundworker to finish the download.
while (this.backgroundworker1.isbusy)
{
// keep ui messages moving, so the form remains
// responsive during the asynchronous operation.
application.doevents();
}
// the download is done, so enable the button.
this.dowloadbutton.enabled = true;
}
显示结果
backgroundworker1_runworkercompleted 方法将处理 runworkercompleted 事件,并在后台操作完成后被调用。它首先检查 asynccompletedeventargs.error 属性,如果该属性是 null,它将显示文件内容。
复制代码 代码如下:
private void backgroundworker1_runworkercompleted(
object sender,
runworkercompletedeventargs e)
{
if (e.error == null)
{
messagebox.show(document.innerxml, "download complete");
}
else
{
messagebox.show(
"failed to download file",
"download failed",
messageboxbuttons.ok,
messageboxicon.error );
}
}
上一篇: 使用java实现各种数据统计图(柱形图,饼图,折线图)
下一篇: android通知栏的实现方法分析