Example #1
0
        private void btn_checkUpdateNow_Click(object sender, EventArgs e)
        {
            if (_versionChecker != null && _versionChecker.IsBusy) return;
            //btn_checkUpdateNow.Enabled = false;
            btn_checkUpdateNow.Text = "";
            btn_checkUpdateNow.Image = Resources.checking;
            //picture_checking.Visible = true;
            lb_Version.Text = Application.ProductVersion;

            errorProvider.Clear();


            if (_versionChecker == null)
            {

                _versionChecker = new VersionChecker(AppSettings.CheckForUpdateUrl);

                _versionChecker.Finished += version =>
                {
                    if (btn_checkUpdateNow.IsDisposed) return;

                    btn_checkUpdateNow.Invoke(new Action(() =>
                    {
                        //btn_checkUpdateNow.Enabled = true;
                        btn_checkUpdateNow.Text = "立即检查";
                        btn_checkUpdateNow.Image = null;
                        //picture_checking.Visible = false;
                        lb_Version.Text = Application.ProductVersion;
                        lb_Version.ForeColor = Color.DarkGray;

                        if (version == null)
                        {
                            errorProvider.SetIconPadding(btn_checkUpdateNow, 4);
                            errorProvider.SetError(btn_checkUpdateNow, "可能由于服务器错误,检查更新失败");
                            return;
                        }


                        if (version.Version != Application.ProductVersion)
                        {
                            using (var frm = new UpdateInfoForm(AppSettings.ProductHomePage, version))
                            {
                                frm.ShowDialog();
                            }
                        }
                        else
                        {
                            lb_Version.Text = Application.ProductVersion + " (已为最新)";
                            lb_Version.ForeColor = Color.CornflowerBlue;

                        }

                    }));
                };

                _versionChecker.ErrorHappened += exception =>
                {
#if DEBUG
                    Console.WriteLine(exception);
#endif

                    btn_checkUpdateNow.Invoke(new Action(() =>
                    {
                        //btn_checkUpdateNow.Enabled = true;
                        btn_checkUpdateNow.Text = "立即检查";
                        btn_checkUpdateNow.Image = null;
                        //picture_checking.Visible = false;


                        errorProvider.SetIconPadding(btn_checkUpdateNow, 4);
                        errorProvider.SetError(btn_checkUpdateNow, string.Format("可能由于网络错误,检查更新失败: \n{0}", exception.Message));
                    }));
                };

            }


            _versionChecker.CheckAsync();

        }
Example #2
0
        //仅在启动一段时间后检查一次更新,
        private static void ScheduledUpdateCheck(object sender, NotifyIcon tray)
        {
            if (!config.Get<bool>(ConfigKeys.AutoCheckForUpdate)) return;
            
            var checker = new VersionChecker(AppSettings.CheckForUpdateUrl);
            checker.Finished += info =>
            {
                var whatsNew = info.WhatsNew.Length > 50 ? info.WhatsNew.Substring(0, 50) : info.WhatsNew;
                
                if (info.Version != Application.ProductVersion)
                {
                    tray.BalloonTipClicked += (o, args) =>
                    {
                        if (info.Version == Application.ProductVersion) return;
                        using (var frm = new UpdateInfoForm(ConfigurationManager.AppSettings.Get(Constants.ProductHomePageAppSettingKey), info))
                        {
                            frm.ShowDialog();
                            tray.Visible = config.Get(ConfigKeys.TrayIconVisible, true);
                        }
                    };
                    if (!tray.Visible)
                    {
                        tray.Visible = true;
                    }
                    
                    tray.ShowBalloonTip(1000 * 15, Application.ProductName + "新版本可用!", "版本:" + info.Version + "\n" + whatsNew, ToolTipIcon.Info);
                }

                checker.Dispose();
                checker = null;

                GC.Collect();
            };
            checker.ErrorHappened += e =>
            {
                Debug.WriteLine("Program.ScheduledUpdateCheck Error:" + e.Message);
                checker.Dispose();
                checker = null;

                GC.Collect();
            };

            checker.CheckAsync();
        }