コード例 #1
0
        //仅在启动一段时间后检查一次更新,
        private static void ScheduledUpdateCheck(object sender, NotifyIcon tray)
        {
            var timer = sender as Timer;

            timer.Stop();
            timer.Dispose();
            timer = null;

            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.ShowBalloonTip(1000 * 15, Application.ProductName + "新版本可用!", "版本:" + info.Version + "\n" + whatsNew, ToolTipIcon.Info);
                }

                checker.Dispose();
                checker = null;

                GC.Collect();
            };

#if DEBUG
            checker.ErrorHappened += e =>
            {
                Debug.WriteLine("Program.ScheduledUpdateCheck Error:" + e.Message);
                checker.Dispose();
                checker = null;

                GC.Collect();
            };
#endif

            checker.CheckAsync();
        }