zoukankan      html  css  js  c++  java
  • C#子线程中更新ui-----c# 多线程多文件批量下载

    c# 多线程多文件批量下载

     

    废话少说,先演示一张效果图

    简单说下过程喽

    开发过程中其实总是会碰到项目想应用下载文件~

    看其他语言有很多封装好的类库可以使用~~

    作为小白的我并没有找到很多c#的案例可参考

    后找到一款“MutThreadDownLoadFile”的demo

    但是每次使用感觉并不方便,另外可用信息可扩展不是特别强。。

    然后重新改进并封装了类库的形式,可以简单使用

    贴出此demo的代码看一下

    复制代码
     public Form1()
            {
                InitializeComponent();
            }
            DownLoadFile dlf = new DownLoadFile();
            private void btnTest_Click(object sender, EventArgs e)
            {
                string[] lines = File.ReadAllLines("华军软件.txt");
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] line = lines[i].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    if (line.Length == 2)
                    {
                        string path = Uri.EscapeUriString(line[1]);
                        string filename = Path.GetFileName(path);
                        string dir = @"D:	est";
                        ListViewItem item = listView1.Items.Add(new ListViewItem(new string[] { (listView1.Items.Count + 1).ToString(), filename, "0", "0", "0%", "0", "0", DateTime.Now.ToString(), "等待中", line[1] }));
                        int id = item.Index;
                        dlf.AddDown(path, dir, "", id);
                    }
                }
                dlf.StartDown();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                dlf.ThreadNum = 3;//线程数,不设置默认为3
                dlf.doSendMsg += SendMsgHander;//下载过程处理事件
            }
            private void SendMsgHander(DownMsg msg)
            {
                switch (msg.Tag)
                {
                    case DownStatus.Start:
                        this.Invoke((MethodInvoker)delegate ()
                        {
                            listView1.Items[msg.Id].SubItems[8].Text = "开始下载";
                            listView1.Items[msg.Id].SubItems[7].Text = DateTime.Now.ToString();
                        });
                        break;
                    case DownStatus.GetLength:
                        this.Invoke((MethodInvoker)delegate ()
                        {
                            listView1.Items[msg.Id].SubItems[3].Text = msg.LengthInfo;
                            listView1.Items[msg.Id].SubItems[8].Text = "连接成功";
                        });
                        break;
                    case DownStatus.End:
                    case DownStatus.DownLoad:
                        this.Invoke(new MethodInvoker(() =>
                        {
                            this.Invoke((MethodInvoker)delegate ()
                            {
                                listView1.Items[msg.Id].SubItems[2].Text = msg.SizeInfo;
                                listView1.Items[msg.Id].SubItems[4].Text = msg.Progress.ToString()+"%";
                                listView1.Items[msg.Id].SubItems[5].Text = msg.SpeedInfo;
                                listView1.Items[msg.Id].SubItems[6].Text = msg.SurplusInfo;
                                if (msg.Tag == DownStatus.DownLoad)
                                {
                                    listView1.Items[msg.Id].SubItems[8].Text = "下载中";
                                }
                                else
                                {
                                    listView1.Items[msg.Id].SubItems[8].Text = "下载完成";
                                }
                                Application.DoEvents();
                            });
                        }));
                        break;
                    case DownStatus.Error:
                        this.Invoke((MethodInvoker)delegate ()
                        {
                            listView1.Items[msg.Id].SubItems[6].Text = "失败";
                            listView1.Items[msg.Id].SubItems[8].Text = msg.ErrMessage;
                            Application.DoEvents();
                        });
                        break;
                }
            }
    复制代码

    应用时不考虑下载过程

    只需要添加下载的文件

    设定好下载线程以及最多每次下载几个文件(默认为3)即可

    希望这次的封装,可以帮到其他需要应用到该类库的朋友

    点击下载

      

    注意:在使用多线程时,记得每个线程之间 间隔几秒,如Thread.Sleep(5000);,在下载使用的时候,记得添加。

    由于时间的关系,只是花了两天的时间测试和完善,可能很多地方考虑的并不是很周全(暂时没发现太明显bug)

    大家可以应用并测试后反馈在这个地方,方便我们一起更好的完善它!

    原文链接:http://www.cnblogs.com/jianzhan/p/7137485.html

  • 相关阅读:
    LVS基于DR模式负载均衡的配置
    Linux源码安装mysql 5.6.12 (cmake编译)
    HOSt ip is not allowed to connect to this MySql server
    zoj 3229 Shoot the Bullet(无源汇上下界最大流)
    hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
    poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
    URAL 1430 Crime and Punishment
    hdu 2048 神、上帝以及老天爷(错排)
    hdu 3367 Pseudoforest(最大生成树)
    FOJ 1683 纪念SlingShot(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8350084.html
Copyright © 2011-2022 走看看