Пример #1
0
        /// <summary>
        /// 传输文件,多线程传输文件,默认最大20个;
        /// </summary>
        static public void TransFile()
        {
            BitsFileList.MoveFirst();
            int     i = 0, imax = _maxFiles * 100;
            XmlNode nodeFile = null;

            while (!BitsFileList.IsEOF() && i < imax)
            {
                i++;
                nodeFile = BitsFileList.GetCurrentFile();
                if (null == nodeFile)
                {
                    break;
                }
                string strState = ((XmlElement)nodeFile).GetAttribute("state");
                if ("moving" == strState)
                {
                    continue;
                }
                if ("doing" == strState)
                {
                    nodeFile.Attributes.RemoveNamedItem("state");
                    BitsFileList.MoveNext();
                    continue;
                }
                ((XmlElement)nodeFile).SetAttribute("state", "moving");
                ThreadPool.QueueUserWorkItem(new WaitCallback(HandleBits));
            }
            if (null != nodeFile)
            {
                nodeFile.Attributes.RemoveNamedItem("state");
            }
            BitsFileList.MoveFirst();
            i = 0;
            while (!BitsFileList.IsEOF() && i < imax)
            {
                i++;
                nodeFile = BitsFileList.GetCurrentFile();
                if (null != nodeFile)
                {
                    nodeFile.Attributes.RemoveNamedItem("state");
                }
                BitsFileList.MoveNext();
            }
            BitsFileList.SaveFile();
        }
Пример #2
0
        /// <summary>
        /// 检查/启动/结束传输作业进程,参数文件节点为空的取当前文件节点
        /// </summary>
        /// <param name="nodeFile">要管理的传输文件节点</param>
        static private void HandleBits(Object o)
        {
            IBackgroundCopyManager bcm = null;
            IBackgroundCopyJob     job = null;
            XmlNode nodeFile           = null;

            nodeFile = BitsFileList.GetCurrentFile();
            if (null == nodeFile)
            {
                return;
            }
            ((XmlElement)nodeFile).SetAttribute("state", "doing");
            try
            {
                // Create BITS object
                bcm = (IBackgroundCopyManager) new BackgroundCopyManager();
                Guid jobID = Guid.Empty;
                if (null != nodeFile.Attributes["jobguid"] && !string.IsNullOrEmpty(nodeFile.Attributes["jobguid"].Value))
                { // Do we already have a job in place?
                    jobID = new Guid(nodeFile.Attributes["jobguid"].Value);
                    BG_JOB_STATE state;
                    try
                    {
                        bcm.GetJob(ref jobID, out job); // Get the BITS job object
                        job.GetState(out state);        // check its state
                        switch (state)
                        {
                        case BG_JOB_STATE.BG_JOB_STATE_ERROR:     // If it is an error, re-poll
                            job.Complete();
                            nodeFile.Attributes.RemoveNamedItem("jobguid");
                            Marshal.ReleaseComObject(job);
                            job = null;
                            break;

                        case BG_JOB_STATE.BG_JOB_STATE_CANCELLED:
                        case BG_JOB_STATE.BG_JOB_STATE_TRANSFERRED:     // If we got the job
                            job.Complete();                             // then complete it
                            nodeFile.ParentNode.RemoveChild(nodeFile);
                            Marshal.ReleaseComObject(job);
                            Marshal.ReleaseComObject(bcm);
                            return;

                        default:
                            Marshal.ReleaseComObject(bcm);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        NameValueCollection errInfo = new NameValueCollection();
                        errInfo["文件类别"]   = nodeFile.Attributes["doctype"].Value;
                        errInfo["远程文件"]   = nodeFile.Attributes["srcurl"].Value;
                        errInfo["作业Guid"] = nodeFile.Attributes["jobguid"].Value;
                        ExceptionManager.Publish(e, errInfo);
                        if (null != (e as UnauthorizedAccessException))
                        {
                            if (job != null)
                            {
                                Marshal.ReleaseComObject(job);
                            }
                            if (bcm != null)
                            {
                                Marshal.ReleaseComObject(bcm);
                            }
                            return;
                        }
                        COMException exCOM = e as COMException;
                        if (null != exCOM && exCOM.ErrorCode == unchecked ((Int32)0x80200001))
                        {
                            nodeFile.Attributes.RemoveNamedItem("jobguid");
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                // Create a bits job to download the next expected update
                if (null != nodeFile && (null == nodeFile.Attributes["jobguid"] || string.IsNullOrEmpty(nodeFile.Attributes["jobguid"].Value)))
                {
                    bcm.CreateJob("下载远程文件",
                                  BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, out jobID, out job);
                    string doctype = nodeFile.Attributes["doctype"].Value;
                    string srcurl  = nodeFile.Attributes["srcurl"].Value;
                    string dest    = nodeFile.Attributes["localname"].Value;

                    job.SetDescription("下载文件位置: " + doctype);
                    job.AddFile(srcurl, dest);
                    job.Resume(); // start the job in action
                    ((XmlElement)nodeFile).SetAttribute("jobguid", jobID.ToString());
                }
                if (bcm != null)
                {
                    Marshal.ReleaseComObject(bcm);
                }
                return;
            }
            catch { }
        }