public bool Add(string guId, DownLoadFileOoj_Client obj)
 {
     DownLoadFileOoj_Client value = null;
     if (PCClientDownLoadFilesDic.TryGetValue(guId, out value))
     {
       return  PCClientDownLoadFilesDic.TryUpdate(guId, obj, value);
     }
     else
     {
       return  PCClientDownLoadFilesDic.TryAdd(guId, obj);
     }
 }
       // public static bool GetFile(string path, string fileName, string guId, out string msg)
       //{
       //    if (GlobalStaticObj.Instance.proxyFile.DownLoadFile(path, fileName, guId, out msg))
       //    {
       //        DownLoadFileOoj_Client fileObj = new DownLoadFileOoj_Client();
       //        fileObj.FileName = fileName;
       //        fileObj.guId = guId;
       //        fileObj.DownLoadStatus = GetFileDownLoadStatus.waiting;
       //        DownLoadFilesQueue.Instance.Add(guId, fileObj);
       //        DownLoadFileMsg(new DownLoadFileEventArgs(guId,fileObj));
       //        loadData(path, fileName, guId);
       //        return true;
       //    }
       //    return false;
       //}

        private static void ThreadDownLoadMethod(object ParObject)
        {
            FileThreadPramObj o = (FileThreadPramObj)ParObject;
            DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
            DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0);
            fileObj0.DownLoadStatus = GetFileDownLoadStatus.StartDownLoad;
            fileObj0.DownLoadSize = 0;
            DownLoadFilesQueue.Instance.Add(o.guId, fileObj0);
            DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObj0));
            bool firstWrite = true;
            while (true)
            {
                if (!firstWrite)
                {
                    DownLoadFileOoj_Client fileObj1 = new DownLoadFileOoj_Client();
                    DownLoadFilesQueue.Instance.Get(o.guId, out fileObj1);
                    if ((System.DateTime.Now - fileObj1.getFileTime).TotalMinutes > 3)//写入等待超时3分钟
                    {
                        o.myThread.Abort();
                        o.myThread = null;
                    }
                }
                DownLoadFileDataObj obj = new DownLoadFileDataObj();
                if (GlobalStaticObj.Instance.proxyFile.GetSucDataBlock(o.guId,UserId, PCClientCookieStr))
                {
                    string resultStr = GlobalStaticObj.Instance.proxyFile.DownLoadFileDataBlock(o.guId,UserId, PCClientCookieStr);
                    if (!string.IsNullOrEmpty(resultStr))
                    {
                        DownLoadFileDataObj dataObj = JsonConvert.DeserializeObject<DownLoadFileDataObj>(resultStr);
                        if (dataObj != null)
                        {
                            DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0);
                            firstWrite = false;
                            DownLoadFilesBlocksQueue.Instance.Add(dataObj);
                            fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoading;
                            fileObj0.DownLoadSize = fileObj0.DownLoadSize + dataObj.CanReadLength;
                            fileObj0.getFileTime = System.DateTime.Now;
                            if (fileObj0.DownLoadSize == fileObj0.FileSize)
                            {
                                fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
                            }
                            DownLoadFilesQueue.Instance.Add(o.guId, fileObj0);
                            DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObj0));
                            if (fileObj0.DownLoadSize == fileObj0.FileSize)
                            {
                                Thread.Sleep(300);
                                o.myThread.Abort();
                                o.myThread = null;
                            }
                        }
                    }
                }
            }
        }
 public static bool ClientDownLoadFile(string path, string fileName, string guId, out string msg, out long FileSize)
 {
     if (WCFClientProxy.TestPCClientProxy())
     {
         if (GlobalStaticObj.Instance.proxyFile.DownLoadFile(path, fileName, guId, UserId, PCClientCookieStr, out msg, out FileSize))
         {
             DownLoadFileOoj_Client fileObj = new DownLoadFileOoj_Client();
             fileObj.FileName = fileName;
             fileObj.guId = guId;
             fileObj.FileSize = FileSize;
             fileObj.path = path;
             fileObj.NextDataBlockNum = 1;
             fileObj.DownLoadStatus = GetFileDownLoadStatus.waiting;
             DownLoadFilesQueue.Instance.Add(guId, fileObj);
             DownLoadFileMsg(new DownLoadFileEventArgs(guId, fileObj));
             lock (mylock3)
             {
                 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadDownLoadMethod);
                 Thread myThread = new Thread(ParStart);
                 myThread.IsBackground = true;
                 FileThreadPramObj o = new FileThreadPramObj();
                 o.guId = guId;
                 o.myThread = myThread;
                 myThread.Start(o);
             }
             return true;
         }
         return false;
     }
     else {
         msg = "无法连接服务端";
         FileSize = 0;
         return false;
     
     }
 }
        //ThreadMethod如下:
        public void ThreadWriteFileMethod(object ParObject)
        {
            FileThreadPramObj o = (FileThreadPramObj)ParObject;
            DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
            DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0);
            fileObj0.Status = GetFileWriteStatus.StartWrite;
            DownLoadFilesQueue.Instance.Add(o.guId, fileObj0);
            HXCFileTransferCache_Client.FileTransferClient.DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObj0));
            FileStream fsWrite = null;
            long fileSize = 0;
            bool firstWrite = true;
            DownLoadFileOoj_Client fileObjAim = fileObj0;
            while (true)
            {
            start:
                if (!firstWrite)
                {
                    DownLoadFileOoj_Client fileObj1 = new DownLoadFileOoj_Client();
                    DownLoadFilesQueue.Instance.Get(o.guId, out fileObj1);
                    fileObjAim = fileObj1;

                    if ((System.DateTime.Now - fileObjAim.writeTime).TotalMinutes > 3)//写入等待超时3分钟
                    {
                        if (fsWrite != null)
                        {
                            fsWrite.Close();
                            fsWrite.Dispose();
                        }
                        //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
                        fileObjAim.Status = GetFileWriteStatus.WriteWaitTimeOut;
                        DownLoadFilesQueue.Instance.Add(o.guId, fileObjAim);
                        HXCFileTransferCache_Client.FileTransferClient.DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObjAim));
                        //Utility.Log.Log.writeLineToLog("写入线程终止--Rest点:" + block.Order_Num.ToString(), "Write" + guId);
                        Thread.Sleep(300);
                        o.myThread.Abort();
                        o.myThread = null;
                    }
                }
            for (int i = 0; i < PCClientDownLoadFileBlocksBag.Count; i++)
                {
                    DownLoadFileDataObj block = PCClientDownLoadFileBlocksBag.ElementAt<DownLoadFileDataObj>(i);
                    if (block.Order_Num != fileObjAim.NextDataBlockNum)
                    {
                        continue;
                    }
                    if (o.guId != block.guId)
                    {
                        continue;
                    }
                    if (!Directory.Exists(DownLoadFilesBlocksQueue.Instance.SavePath))//存放的默认文件夹是否存在
                    {
                        Directory.CreateDirectory(Instance.SavePath);//不存在则创建
                    }
                    string fileFullPath = Path.Combine(Application.StartupPath + "\\"+DownLoadFilesBlocksQueue.Instance.SavePath, fileObjAim.FileName);//合并路径生成文件存放路径 
                    if (fsWrite == null)
                    {
                        if (block.Order_Num == 1)
                        {
                            fsWrite = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.Write);
                            firstWrite = false;
                        }
                        else
                        {
                            fsWrite = new FileStream(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
                        }
                    }                  
                    fsWrite.Write(block.FileData, 0, block.CanReadLength);
                    fileSize = fileSize + block.CanReadLength;    
                    fileObjAim.Status = GetFileWriteStatus.Writing;
                    fileObjAim.writeTime = System.DateTime.Now;
                    if (fileSize == fileObjAim.FileSize)//完整写入
                    {
                        fsWrite.Close();
                        fsWrite.Dispose();
                        //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
                        fileObjAim.WriteSize = fileObjAim.WriteSize + block.CanReadLength;
                        fileObjAim.Status = GetFileWriteStatus.WriteEnd;
                        DownLoadFilesQueue.Instance.Add(o.guId, fileObjAim);
                        HXCFileTransferCache_Client.FileTransferClient.DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObjAim));
                        GlobalStaticObj.Instance.proxyFile.DeleteServerFileId(o.guId, FileTransferClient.UserId, FileTransferClient.PCClientCookieStr);
                        DownLoadFilesQueue.Instance.Remove(o.guId, out fileObjAim);
                        //Utility.Log.Log.writeLineToLog("写入线程终止--Rest点:" + block.Order_Num.ToString(), "Write" + guId);
                        Thread.Sleep(300);
                        o.myThread.Abort();
                        o.myThread = null;
                    }
                    fileObjAim.WriteSize = fileObjAim.WriteSize + block.CanReadLength;
                    fileObjAim.NextDataBlockNum = fileObjAim.NextDataBlockNum + 1;
                    DownLoadFilesQueue.Instance.Add(o.guId, fileObjAim);
                    HXCFileTransferCache_Client.FileTransferClient.DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObjAim));
                    //ServerFilesBlocksQueue.TryDequeue(out block);
                    PCClientDownLoadFileBlocksBag.TryTake(out block);
                    //Thread.Sleep(50);
                }
                goto start;
            }
        }
 public bool Get(string guId, out DownLoadFileOoj_Client obj)
 {
     return PCClientDownLoadFilesDic.TryGetValue(guId, out obj);
 }
 public bool Remove(string guId, out DownLoadFileOoj_Client obj)
 {
     return PCClientDownLoadFilesDic.TryRemove(guId, out obj);
 }