Пример #1
0
 static void Main(string[] args)
 {
     #region 嵌入的DLL加载
     AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
     {
         string dllName = e.Name.Contains(",") ? e.Name.Substring(0, e.Name.IndexOf(',')) : e.Name.Replace(".dll", "");
         dllName = dllName.Replace(".", "_");
         if (dllName.EndsWith("_resources"))
         {
             return(null);
         }
         System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Upgrade.Program.Client.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
         byte[] bytes = (byte[])rm.GetObject(dllName);
         return(System.Reflection.Assembly.Load(bytes));
     };
     #endregion
     #region 异常处理
     //处理未捕获的异常
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
     //处理UI线程异常
     Application.ThreadException += (s, e) =>
     {
         if (e.Exception != null)
         {
             MessageBox.Show(e.Exception.ToString());
         }
     };
     //处理非UI线程异常
     AppDomain.CurrentDomain.UnhandledException += (s, e) =>
     {
         var ex = e.ExceptionObject as Exception;
         if (ex != null)
         {
             MessageBox.Show(ex.ToString());
         }
     };
     #endregion
     //MessageBox.Show(string.Join("\r\n", args));
     //args = new string[] { "-v", "true", "-u", "http://localhost:9099/api/UpgradeServer", "-pipe", "UpgradeClient", "-um", "54b022e6-5069-4330-8fd6-6c2285903034" };
     Commands.Init(args);
     if (args.Contains("/?") || args.Contains("?"))
     {
         MessageBox.Show(Commands.GetInfo());
     }
     else
     {
         Commands.CommandPipe.Listen();
         //Commands.CommandMainMutex.WaitMainClose();
         if (Commands.CommandMutex.GetMutexFlag())
         {
             if (Commands.CommandVisible.GetVisible())
             {
                 try
                 {
                     Application.EnableVisualStyles();
                     Application.SetCompatibleTextRenderingDefault(false);
                     UpgradeApplication.Run();
                 }
                 catch
                 {
                 }
             }
             else
             {
                 Commands.CommandPipe.Listen();
                 Application.Run();
             }
         }
     }
 }
Пример #2
0
        public void Listen()
        {
            if (this.Value == null)
            {
                return;
            }
            this.pipeServer = new PipeServer(this.Value);
            this.pipeServer.Listen();
            this.pipeServer.ReceivePipeData += async(b) =>
            {
                //if (PipeUpgradeCommand.TryFromBytes(b, out PipeUpgradeCommand pipeCommand))
                //{
                //    switch (pipeCommand.Command)
                //    {
                //        case PipeUpgradeCommand.Commands.Colse:
                //            Application.Exit();
                //            break;
                //    }
                //    return;
                //}
                if (PipeUpgradeUI.TryFromBytes(b, out PipeUpgradeUI pipeUpgradeUI))
                {
                    if (pipeUpgradeUI.UIImage != null)
                    {
                        try
                        {
                            using (MemoryStream ms = new MemoryStream(pipeUpgradeUI.UIImage))
                            {
                                UpgradeApplication.SetUIRenderer(new UpgradeUIRenderer()
                                {
                                    BackgroundImage = Bitmap.FromStream(ms),
                                    BackgroundColor = System.Drawing.ColorTranslator.FromHtml(pipeUpgradeUI.ColorHex),
                                    Size            = new Size(pipeUpgradeUI.Width, pipeUpgradeUI.Height),
                                    Company         = pipeUpgradeUI.Company,
                                    Copyright       = pipeUpgradeUI.Copyright,
                                    Version         = pipeUpgradeUI.Version
                                });
                            }
                        }
                        catch (Exception ex) { }
                    }
                    return;
                }
                if (PipeUpgradeCheckVersion.TryFromBytes(b, out PipeUpgradeCheckVersion pipeCheckVersion))
                {
                    JsonReleaseVersion jsonReleaseVersion = await Commands.CommandUrl.CheckVersion(pipeCheckVersion.Version);

                    if (jsonReleaseVersion != null)
                    {
                        if (await Commands.CommandUrl.Upgrade(jsonReleaseVersion))
                        {
                            UpgradeApplication.SetMessage("升级完成");
                        }
                        else
                        {
                            UpgradeApplication.SetMessage("升级失败");
                        }
                    }
                    else
                    {
                        UpgradeApplication.SetMessage("无法获取升级信息");
                    }
                    await Task.Delay(1000);

                    if (await Commands.CommandMainRestart.MainStart())
                    {
                        Application.Exit();
                    }
                    else
                    {
                        UpgradeApplication.SetMessage("无法打开主程序");
                        await Task.Delay(1000);

                        Application.Exit();
                    }
                }
            };
        }