多线程下载

简单得 使用线程池,和WebClient 结合下载资源到本地

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

public class TheadEvent
{
    public string url { set; get; }
    public string savePath { set; get; }
    public Stopwatch sw = new Stopwatch();
}
public class TestDownLoad : MonoBehaviour {
    public Action<byte[]> actionImage;
    public RawImage img;
    private Stopwatch sw = new Stopwatch();
    private readonly object sysob = new object();
    int flag;

    // Use this for initialization
    void Start () {

        ThreadPool.SetMaxThreads(2, 2);
        ThreadPool.SetMinThreads(1, 1);
        TheadEvent t1 = new TheadEvent();
        t1.url= "http://127.0.0.1/0.mp4";
        t1.savePath = "E:\\testFile\\0.mp4";
        TheadEvent t2 = new TheadEvent();
        t2.url = "http://127.0.0.1/1.mp4";
        t2.savePath = "E:\\testFile\\1.mp4";
        TheadEvent t3 = new TheadEvent();
        t3.url = "http://127.0.0.1/2.mp4";
        t3.savePath = "E:\\testFile\\2.mp4";
        TheadEvent t4 = new TheadEvent();
        t4.url = "http://127.0.0.1/购票信息.txt";
        t4.savePath = "E:\\testFile\\购票信息.txt";
        TheadEvent t5 = new TheadEvent();
        t5.url = "http://127.0.0.1/weixin_1220.apk";
        t5.savePath = "E:\\testFile\\weixin_1220.apk";
        TheadEvent t6 = new TheadEvent();
        t6.url = "http://127.0.0.1/5.png";
        t6.savePath = "E:\\testFile\\5.png";
        Add(t1);
        Add(t2);
        Add(t3);
        Add(t4);
        Add(t5);
        Add(t6);
        //for (int i=0;i<60;i++)
        // {
        //    ThreadPool.QueueUserWorkItem(new WaitCallback(Computer) , i.ToString());
        // }
        UnityEngine.Debug.Log("主线程结束");

    }
    void Computer(object ob)
    {
        lock(sysob)
        {
            int b = 0;
            UnityEngine.Debug.Log("_线程:" + ob.ToString() + "_执行时间:" + DateTime.UtcNow);
            for (int i = 0; i < 10000; i++)
            {
                b += i;
            }
            UnityEngine.Debug.Log("_线程:" + ob.ToString() + "_计算结果:" + b);
        }
    }
    void Add(TheadEvent threadEvent)
    {
      ThreadPool.QueueUserWorkItem(new WaitCallback(OnDownLoad), threadEvent);
    }

    void OnDownLoad(object ob)
    {
        lock(sysob)
        {
            TheadEvent threadEvent = ob as TheadEvent;
            using (WebClient webClient = new WebClient())
                {
                HttpWebRequest request = new HttpWebRequest(new Uri(threadEvent.url));

                    threadEvent.sw.Start();                                
                    webClient.DownloadProgressChanged += DownloadProgressChanged;
                   // webClient.DownloadDataAsync(new Uri(threadEvent.url), threadEvent);
                   // webClient.DownloadDataCompleted += WebClient_DownloadDataCompleted;
                    webClient.DownloadFileAsync(new Uri(threadEvent.url), threadEvent.savePath,threadEvent);
                   // UnityEngine.Debug.Log("_线程:" + ob.ToString());

                }
     Thread.Sleep(200);
        }  
    }

    private void WebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        TheadEvent t = e.UserState as TheadEvent;
        byte[] bs=  e.Result;

        UnityEngine.Debug.Log(t.url+"_内容:"+Encoding.UTF8.GetString(bs) );
    }

    private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        TheadEvent t = e.UserState as TheadEvent;
        string value = (e.BytesReceived / 1024d / t.sw.Elapsed.TotalSeconds).ToString();
        UnityEngine.Debug.Log(t.url+"下载速度:"+value + "/kb");
        if (e.ProgressPercentage==100&&e.BytesReceived==e.TotalBytesToReceive)
        {
            t.sw.Reset();          
            UnityEngine.Debug.LogError("downLoad Over:"+ t.url);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tianyongheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值