コード例 #1
0
ファイル: DownloadMgr.cs プロジェクト: omnana/MapProject
    /// <summary>
    /// 异步会创建线程
    /// </summary>
    /// <param name="info"></param>
    public void DownloadAsync(DownloadUnit info)
    {
        if (info == null)
        {
            return;
        }

        var fileMac = new DownloadFileMac(info);

        lock (mlock)
        {
            readyList.Enqueue(fileMac);
        }

        if (runningList.Count < MAX_THREAD_COUNT)
        {
            var thread = new Thread(ThreadLoop);

            lock (mlock)
            {
                runningList.Add(thread, null);
            }

            thread.Start();
        }
    }
コード例 #2
0
ファイル: DownloadMgr.cs プロジェクト: omnana/MapProject
    /// <summary>
    /// 同步不会调用回调函数,返回是否成功
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    public bool DownloadSync(DownloadUnit info)
    {
        if (info == null)
        {
            return(false);
        }

        var mac = new DownloadFileMac(info);

        try // 同步下载尝试三次
        {
            mac.Run();
            if (mac.State == DownloadMacState.Complete)
            {
                return(true);
            }
            mac.Run();
            if (mac.State == DownloadMacState.Complete)
            {
                return(true);
            }
            mac.Run();
            if (mac.State == DownloadMacState.Complete)
            {
                return(true);
            }
        }
        catch (Exception ex)
        {
            Debug.Log("Error DownloadSync " + mac.State + " " + mac.DownUnit.Name + " " + ex.Message + " " + ex.StackTrace);
        }

        return(false);
    }
コード例 #3
0
ファイル: DownloadMgr.cs プロジェクト: omnana/MapProject
    private void ThreadLoop()
    {
        while (true)
        {
            DownloadFileMac mac = null;

            lock (mlock)
            {
                if (readyList.Count > 0)
                {
                    mac = readyList.Dequeue();

                    runningList[Thread.CurrentThread] = mac;

                    if (mac != null && mac.DownUnit.IsDelete) // 已经销毁,不提取运行,直接删除
                    {
                        runningList[Thread.CurrentThread] = null;

                        continue;
                    }
                }
            }

            // 已经没有需要下载的了
            if (mac == null)
            {
                break;
            }

            mac.Run();

            if (mac.State == DownloadMacState.Complete)
            {
                lock (mlock)
                {
                    completeList.Add(mac.DownUnit);

                    runningList[Thread.CurrentThread] = null;
                }
            }
            else if (mac.State == DownloadMacState.Error)
            {
                lock (mlock)
                {
                    readyList.Enqueue(mac);
                    // 防止失败频繁回调,只在特定次数回调
                    if (mac.IsNeedErrorCall())
                    {
                        errorList.Add(mac);
                    }
                }

                break;
            }
            else
            {
                //ThreadDebugLog.Log("Error DownloadMacState " + mac.State + " " + mac.DownUnit.name);
                break;
            }
        }
    }