webClient类,异步的时候发生的webapi 异常处理是怎么回事

匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。sponsored links
WebClient.DownloadFile(线程机制,异步下载文件)
& & & 线程机制(避免卡屏),异步下载文件。
  我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件,不妨搜下,不过WebClient.DownloadFile使用更简单)。
  今天简单的演示下WebClient.DownloadFileAsync的使用,刚刚写好的实例,有问题你拍我。
  解释什么的都不需要了吧。这里面应用了线程机制,异步调用。wo先展示代码,在把一些关键点放置到后面,并给一些修改建议(设计器生成代码在下,可以不用看,只需要粘贴使用)。
测试中用到的网络文件(随机抽取.exe,.rar,.zip),都是无害的,请放心测试:
/client/9h162/fzjqd.exehttp://sq.onlinedown.net/down/sgbs.rarhttp://zj./201408/bdswxt_v1.0.ziphttp://zj./201408/zyzfwxt_v1.1.zip
using System.Collections.G
using ponentM
using System.D
using System.D
using System.L
using System.T
using System.Windows.F
using System.N
using System.T
using System.IO;
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "error.log", DateTime.Now.ToString() + "\t" + e.ExceptionObject.ToString() + Environment.NewLine);
/// &summary&
/// 文件总数
/// &/summary&
int _totalFile = 0;
/// &summary&
/// 已下载文件
/// &/summary&
int _loadFile = 0;
private void button1_Click(object sender, EventArgs e)
string richText = richTextBox1.T
if (string.IsNullOrEmpty(richText))
MessageBox.Show("请在文本框中输入需要下载的网络文件");
Thread thread = new Thread(new ThreadStart(() =& createWebClient(richText)));
thread.Start();
//WebClient webClient =
private void createWebClient(string richText)
string[] array = richText.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
_totalFile = array.L
foreach (string item in array)
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
string fileName = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToFileTimeUtc() + item.Substring(item.LastIndexOf('/') + 1);
webClient.DownloadFileAsync(new Uri(item), fileName);
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
this.Invoke(new MethodInvoker(delegate {
this.progressBar2.Value = e.ProgressP
this.label2.Text = string.Format("正在下载文件,完成进度{0}%
{1}/{2}(字节)"
,e.ProgressPercentage
, e.BytesReceived
, e.TotalBytesToReceive);
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
_loadFile++;
int percent = (int)(100.0 * _loadFile / _totalFile);
this.Invoke(new MethodInvoker(delegate {
this.progressBar1.Value =
this.label1.Text = string.Format("已完成文件下载{0}%
{1}/{2}(文件个数)"
, _loadFile
, _totalFile);
if (sender is WebClient)
((WebClient)sender).CancelAsync();
((WebClient)sender).Dispose();
private ponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
private void InitializeComponent()
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// progressBar1
this.progressBar1.Location = new System.Drawing.Point(81, 160);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(330, 12);
this.progressBar1.TabIndex = 0;
// progressBar2
this.progressBar2.Location = new System.Drawing.Point(81, 224);
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(330, 12);
this.progressBar2.TabIndex = 1;
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(59, 187);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(59, 252);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 3;
this.label2.Text = "label2";
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 160);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 4;
this.label3.Text = "总 进 度:";
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 224);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(65, 12);
this.label4.TabIndex = 5;
this.label4.Text = "当前进度:";
// richTextBox1
this.richTextBox1.Location = new System.Drawing.Point(14, 12);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(397, 131);
this.richTextBox1.TabIndex = 6;
this.richTextBox1.Text = "";
// button1
this.button1.Location = new System.Drawing.Point(150, 280);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 7;
this.button1.Text = "Run";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.F
this.ClientSize = new System.Drawing.Size(423, 315);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar2);
this.Controls.Add(this.progressBar1);
this.MaximumSize = new System.Drawing.Size(439, 353);
this.MinimumSize = new System.Drawing.Size(439, 353);
this.Name = "Form1";
this.Text = "WebClient下载实现实例";
this.ResumeLayout(false);
this.PerformLayout();
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ProgressBar progressBar2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
大致看了一下,貌似就以下几句需要新手斟酌,大神请绕行。
第一句: this.Invoke(new MethodInvoker(delegate {
第二句:Thread thread = new Thread(new ThreadStart(() =& createWebClient(richText)));
第三句:if (sender is WebClient)
第一句是关键的一句,不再创建控件的线程中调用/修改控件的属性值(Text),会报异常,为什么这么用就OK了?
第二句,第三句就简单了,不提示了,不会了找度度。
本程序可以添加的功能,添加一个Stop按钮,终断下载(请不要嗤之以鼻,在创建窗体的线程里操作new Thread让其立即终断下载,还是有点深度)
namespace ConsoleAppSyncDownload{
class Program
static void Main(string[] args)
WebClient webClient = new WebClient();
//Console.Write ...
在C#当中,利用WebClient这个核心类,可以轻易的打造一个下载器.但是这里想要强调的是,我们用的是异步操作.所谓异步,是相对于同步的概念而言的.比如Web中的Ajax就是基于异步的.它能够提供良好的用户体验,让用户在进行操作时,不感觉到&卡&(不阻塞UI线程),能够同时进行其它的操作并能够随意的切换到任务界面.在下载文件时,如果文件 ...
在C#当中,利用WebClient这个核心类,可以轻易的打造一个下载器.但是这里想要强调的是,我们用的是异步操作.所谓异步,是相对于同步的概念而言的.比如Web中的Ajax就是基于异步的.它能够提供良好的用户体验,让用户在进行操作时,不感觉到&卡&(不阻塞UI线程),能够同时进行其它的操作并能够随意的切换到任务界面.在下载文件时,如果文件 ...
1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { updateFile = Environme ...
最近一直在把公司游戏迁移到支持ARM64,中间出现了很多BUG或者Unity目前不支持操作的问题,这两天遇到异步HttpWebRequest不支持的问题,因为之前一直没有接触,所以也趁机学习了下相关使用. 下面通过一个例子,来使用HttpWebRequest异步下载一个ZIP文件. using UnityE using System.Collec ...安全检查中...
请打开浏览器的javascript,然后刷新浏览器
< 浏览器安全检查中...
还剩 5 秒&

我要回帖

更多关于 webservice异常处理 的文章

 

随机推荐