锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / C#开源英语 / 简单HTTP文件下载窗体
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
技术分类
讨论组翻译
用Invoke解决跨线程操作无效问题
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。

简单HTTP文件下载窗体

样品图像HTTP文件下载UI

Introduction 介绍

This is a simple user interface for downloading files via HTTP based on Phil Crosby's article located here. 这是一个基于菲尔·克罗斯文章升级而来的例子,实现通过HTTP下载文件,是个简单的用户窗体。

Using the Code 使用的代码

The solution is located in the FileDownloader directory and contains two projects: one is the FileDownlaoder UI library and the other is the WinForms test application. The code leaves much room for improvement, but as is usually the case in this field, the due date for my current project is yesterday; hence it's taken me so long to get a spare minute to post this! Here is how to instantiate the downloader:

解决方案位于FileDownloader目录并包含两个项目:一个是FileDownlaoder UI库,另一个是WinForms应用程序测试。但通常是在这个领域,代码改进的余地大,我目前项目过一段时间也不算什么了;因此,我这么长时间以来才能抽出这一分钟空闲时间!这里是如何实例化下载:

   DownloadURLCollection urls = new DownloadURLCollection();
urls.Add(new DownloadURL("http://www.codeproject.com/cs/internet/
CoolDownloader/CoolDownloader_demo.zip",
@"C:\Temp"));
urls.Add(new DownloadURL("http://releases.mozilla.org/pub/mozilla.org/
firefox/releases/1.5.0.6/win32/en-US/Firefox Setup 1.5.0.6.exe",
@"C:\Temp\newname.zip"));
FileDownloaderForm downloader = new FileDownloaderForm(urls);
downloader.ShowDialog();

That's it! No fuss, no muss. What happens under the hood is very well explained in Phil's article. 就是这样!并不麻烦,菲尔的文章很好的解释了,在底层发生了什么

Points of Interest 兴趣点

Before the world of the BackgroundWorker object in .NET 2.0, in order to get a multi-threaded application to interact with the user interface, you had to invoke the worker threads request on to the UI thread by hand: 在之前的BackgroundWorkerNET 2.0对象中。为了得到一个多线程应用程序与用户窗体交互,您必须调用工作线程请求手工UI线程:

    private void downloader_ProgressChanged(object sender, DownloadEventArgs e)
{
try
{
//since the downloader was started on a different thread, it must be invoked
//back on to the UI thread before we interact with the UI controls
this.Invoke(new MarshalProgress(this.MarshaledProgressChanged), new object[]{
sender, e});
}
catch(Exception ex)
{
DownloaderExceptionManager.Publish(ex);
}
}

When the progress event is fired, it's fired on a different thread. This is so while the file is downloading, the user interface window remains responsive. Before changing the value of the progress bar or the text of a label, the call must be marshaled to the UI thread by calling Invoke on the control that exists on the UI thread, in this case, the FileDownloaderForm. Thus, this.Invoke(...). In the invoke method, you pass a delegate, which is basically an object that can be thought of as a pointer to a function: it "points" to a specific function that conforms to the parameters described by the delegate and returns the value described by the delegate:

当进度事件被触发,是在不同的线程触发。当文件正在下载时,用户窗体窗口仍然响应。在更改进度条的值或一个标签的文本前,调用必须编组到UI线程上,通过存在的控件的Invoke调用来实现,在这种情况下,FileDownloaderForm显示,处理this.Invoke(…)时,您通过一个委托一个对象,可以被认为是一个指向函数的指针:“指”到一个特定的功能,符合委托所描述的参数并返回值所描述的委托:

delegate void MarshalProgress(object sender, DownloadEventArgs e);
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内