好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > asp.net BackgroundWorker之在后臺下載文件

asp.net BackgroundWorker之在后臺下載文件

熱門標簽:銅川小型外呼系統運營商 上海楊浦怎么申請申請400電話 云南外呼電銷機器人系統 海外地圖標注門市標 浙江外呼系統怎么安裝 陜西人工外呼系統哪家好 山西防封卡電銷卡套餐 廈門商鋪地圖標注 地圖標注多個行程
示例:
下面的代碼示例演示如何使用 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 );
}
}
您可能感興趣的文章:
  • ASP.NET Web Api 2實現多文件打包并下載文件的實例
  • ASP.NET(C#) Web Api通過文件流下載文件的實例
  • ASP.NET批量下載文件的方法
  • ASP.NET 在下載文件時對其重命名的思路及實現方法
  • asp.net C#實現下載文件的六種方法實例
  • ASP.NET中下載文件的幾種實例代碼
  • 在ASP.NET中下載文件的實現代碼
  • asp.net 下載文件時根據MIME類型自動判斷保存文件的擴展名
  • asp.net 下載文件時輸出文件內容
  • asp.net Web Services上傳和下載文件(完整代碼)
  • ASP.NET實現從服務器下載文件問題處理

標簽:常州 朔州 自貢 信陽 許昌 萊蕪 孝感 西雙版納

巨人網絡通訊聲明:本文標題《asp.net BackgroundWorker之在后臺下載文件》,本文關鍵詞  asp.net,BackgroundWorker,之在,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net BackgroundWorker之在后臺下載文件》相關的同類信息!
  • 本頁收集關于asp.net BackgroundWorker之在后臺下載文件的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 曰曰啪天天拍视频在线| 国产麻豆精品XXXHD| 师傅轻点灬太粗嗯太深了用力小说 | 教官?你好大?轻点h| 色情哺乳奶水视频网站| 古代公妇高好h| 日本线一线二线三线视频| 日日拍夜夜嗷嗷叫国产| 中文字幕一区三区久久女搜查官| 极品美軳人体GOGO| 肥胖孕妇bbwbbwbbw| 女人与公拘i交酡i免费视频| 快看漫画官网| 91丨露脸丨熟女| 黃色A片三級三級三級免费看血恋| 美女脱了内衣让我吃奶| 午夜dj视频大全观看在线| 茄子污| 人獸交XXXⅩ欧美大片男男| 一级A片猛交BBBB| 免费看挤奶和奶水视频播放| abo肉文| 粗暴h疼哭np各种play| “偷拍” – Page 8 – 亚瑟| 欧美精品AV一级毛片| 高冷气质美女喝酒被啪啪| ww欧洲ww在线播放| 日韩A级毛片免费视频| 97久久精品国产成人影院| 曰批全过程免费视频在线观看| 深夜福利网站在线观看| 极品91尤物被啪到呻吟喷水| 91久久婷婷国产综合精品青草 | 人蛇大战色蛇女三级| 97精品国产AⅤ| 胯下小秘书的呻吟| 国产vivodeshd精品| 999毛片| 国产亚洲一区二区三区在线观看 | 久久聊| 午夜精品久久久久久久久无码小说 |