Exemple #1
0
        /// <summary>
        /// 下载文件回调
        /// </summary>
        /// <param name="msg">消息字符串</param>
        private void SendMsgHander(DownMsg msg)
        {
            switch (msg.Tag)
            {
            case DownStatus.Start:
                Console.WriteLine("开始下载:" + DateTime.Now.ToString());
                ActiveUpdataInfo(EnumUpdataStatus.DownloadFile, "正在连接服务器");
                break;

            case DownStatus.GetLength:
                Console.WriteLine("连接成功:" + msg.LengthInfo);
                ActiveUpdataInfo(EnumUpdataStatus.DownloadFile, "连接成功");
                break;

            case DownStatus.DownLoad:
                if (msg.Tag == DownStatus.DownLoad)
                {
                    Console.Write("下载中:");
                    ActiveUpdataInfo(EnumUpdataStatus.DownloadFile, "下载中" + msg.Progress.ToString() + "%");
                    //proUpdata.Value = Convert.ToInt32(msg.Progress);
                }
                else
                {
                    Console.Write("下载完成:");
                    ActiveUpdataInfo(EnumUpdataStatus.DownloadFile, "下载完成!");
                    //proUpdata.Value = proUpdata.Maximum;
                    isRunStart = false;
                }
                Console.WriteLine(msg.SizeInfo + "\t" + msg.Progress.ToString() + "%\t" + msg.SpeedInfo + "\t" + msg.SurplusInfo);
                break;

            case DownStatus.End:
                Console.WriteLine("下载完成!!!");
                ActiveUpdataInfo(EnumUpdataStatus.DownloadFile, "下载完成!");

                //开始更新
                if (File.Exists(strDownFilePath))
                {
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    try
                    {
                        //解压缩文件
                        ActiveUpdataInfo(EnumUpdataStatus.Updata, "解压缩文件中");

                        //ZipFile.ExtractToDirectory(strDownFilePath, System.AppDomain.CurrentDomain.BaseDirectory);
                        ZipHelper.UnZip(strDownFilePath, System.AppDomain.CurrentDomain.BaseDirectory, "", true);

                        //删除压缩包
                        File.Delete(strDownFilePath);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("更新压缩包操作错误:" + ex.Message);
                        ActiveUpdataInfo(EnumUpdataStatus.Error, "更新压缩包操作错误:" + ex.Message);
                    }
                    watch.Stop();
                    TimeSpan timespan = watch.Elapsed;
                    Console.WriteLine("解压缩执行时间:{0}(毫秒)", timespan.TotalMilliseconds);      //总毫秒数

                    Console.WriteLine("更新完成");
                    ActiveUpdataInfo(EnumUpdataStatus.Done, "更新完成");
                    if (IsAutoRun)
                    {
                        string strRunPath = System.AppDomain.CurrentDomain.BaseDirectory + Run;
                        if (File.Exists(strRunPath))
                        {
                            try
                            {
                                System.Diagnostics.Process.Start(strRunPath);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    isRunStart = false;
                }
                break;

            case DownStatus.Error:
                ActiveUpdataInfo(EnumUpdataStatus.Error, "下载失败:" + msg.ErrMessage);
                Console.WriteLine("下载失败:" + msg.ErrMessage);
                isRunStart = false;
                break;

            default:
                isRunStart = false;
                break;
            }
        }
Exemple #2
0
        private void SendMsgHander(DownMsg msg)
        {
            switch (msg.Tag)
            {
            case DownStatus.Start:
                this.Invoke((MethodInvoker) delegate()
                {
                    Console.WriteLine("开始下载:" + DateTime.Now.ToString());
                    labelInfo.Text = "正在连接服务器";
                });
                break;

            case DownStatus.GetLength:
                this.Invoke((MethodInvoker) delegate()
                {
                    Console.WriteLine("连接成功:" + msg.LengthInfo);
                    labelInfo.Text = "连接成功";
                });
                break;

            case DownStatus.DownLoad:
                this.Invoke(new MethodInvoker(() =>
                {
                    this.Invoke((MethodInvoker) delegate()
                    {
                        if (msg.Tag == DownStatus.DownLoad)
                        {
                            Console.Write("下载中:");
                            labelInfo.Text  = "下载中" + msg.Progress.ToString() + "%";
                            proUpdata.Value = Convert.ToInt32(msg.Progress);
                        }
                        else
                        {
                            Console.Write("下载完成:");
                            proUpdata.Value = proUpdata.Maximum;
                        }
                        Console.WriteLine(msg.SizeInfo + "\t" + msg.Progress.ToString() + "%\t" + msg.SpeedInfo + "\t" + msg.SurplusInfo);
                        Application.DoEvents();
                    });
                }));
                break;

            case DownStatus.End:
                this.Invoke((MethodInvoker) delegate()
                {
                    Console.WriteLine("下载完成!!!");
                    labelInfo.Text  = "下载完成";
                    proUpdata.Value = proUpdata.Maximum;
                    //System.Diagnostics.Process.Start(strDownFilePath);
                });
                //开始更新
                if (File.Exists(strDownFilePath))
                {
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    try
                    {
                        //解压缩文件
                        //ZipFile.ExtractToDirectory(strDownFilePath, System.AppDomain.CurrentDomain.BaseDirectory);
                        ZipHelper.UnZip(strDownFilePath, System.AppDomain.CurrentDomain.BaseDirectory, "", true);

                        //删除压缩包
                        File.Delete(strDownFilePath);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("更新压缩包操作错误:" + ex.Message);
                    }
                    watch.Stop();
                    TimeSpan timespan = watch.Elapsed;
                    Console.WriteLine("解压缩执行时间:{0}(毫秒)", timespan.TotalMilliseconds);      //总毫秒数
                    this.Invoke((MethodInvoker) delegate()
                    {
                        Console.WriteLine("更新完成");
                        labelInfo.Text  = "更新完成";
                        proUpdata.Value = proUpdata.Maximum;
                        if (MessageBox.Show("是否运行更新后的程序?", "更新完成", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "BleTestTool.exe");
                        }
                        Application.Exit();
                    });
                }
                break;

            case DownStatus.Error:
                this.Invoke((MethodInvoker) delegate()
                {
                    Console.WriteLine("下载失败:" + msg.ErrMessage);
                    labelInfo.Text = "下载失败:" + msg.ErrMessage;
                });
                break;

            default:
                break;
            }
        }