欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net BackgroundWorker之在后台下载文件】,下面是详细的讲解!
asp.net BackgroundWorker之在后台下载文件
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());
}
}
关于asp.net BackgroundWorker之在后台下载文件的用户互动如下:
相关问题:asp.net 后台如何控制GridView的样式
答:GridView.Rows[0].Cells[0].Style.Add("BackColor", "#CCCCCC"); BackColor 貌似背景颜色是这个写法,当然你可以换其他的 >>详细
相关问题:asp.net后台改变页面选中项样式
答:后台输出一段js的比如:给你的li加个ID&lt;liid=&quot;li_hlink&quot;&gt;&lt;&#47;li&gt;js代码是functionSetLiClass(){¥(&quot;#li&quot;).addClass(&quot;样式名字&quot;);}然后后台输入的js就是SetLic... >>详细
相关问题:ASP.NET 网页背景的简单问题
答: 直接修改源代码,加上样式,最好的方式就是建立单独的css文件,然后所有网页链接css文件 例如: 路径根据自己的情况调整 很多问题需要修改代码,不要依靠系统带的那些自动功能 >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【asp】asp.net 页面转向 Response.Redirect Ser
- 【jQuery】jQuery实现倒计时跳转的例子-倒计时跳
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
