예제 #1
0
    /// <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);
    }
예제 #2
0
    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;
            }
        }
    }