This project has moved and is read-only. For the latest updates, please go here.

Made an event to know if an update is needed and version number

Aug 26, 2013 at 10:41 PM
Edited Aug 26, 2013 at 10:48 PM
One problem i have found is i can not find out if an update is needed in the code. I created an event that is published after it checks the two versions.

This code goes inside AutoUpdater.cs
bool IsUpdateAvail = false;
            if (CurrentVersion > InstalledVersion)
            {
                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? System.Windows.Forms.Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();

                IsUpdateAvail = true;
            }
            else
            {
                IsUpdateAvail = false;
            }

            // Publish the event
            if (AutoUpdaterEvent != null)
            {
                AutoUpdaterEvent(IsUpdateAvail , CurrentVersion, InstalledVersion);
            }
Here is the event i put after the BackgroundWorkerDoWork
        #region Updater Event

        /// <summary>
        /// Subscribe to receive the event when it is determined whether an update needs to be done or not.
        /// This event will also give the current version and installed version.
        /// This gives you the parameters of the event.
        /// </summary>
        /// <param name="isUpdateAvailable">Flag if there should be and update.</param>
        /// <param name="currentVersion">Current Version available to download.</param>
        /// <param name="installedVersion">Version installed on the system.</param>
        public delegate void AutoUpdaterEventHandler(bool isUpdateAvailable, Version currentVersion, Version installedVersion);

        /// <summary>
        /// Subscribe to receive the event when it is determined whether an update needs to be done or not.
        /// This event will also give the current version and installed version.
        /// 
        /// To subscribe:
        /// AutoUpdater.AutoUpdateEvent += new AutoUpdater.AutoUpdaterEventHandler(method to call);
        /// 
        /// To Unsubscribe:
        /// AutoUpdater.AutoUpdateEvent -= (method to call)
        /// </summary>
        public static event AutoUpdaterEventHandler AutoUpdaterEvent;

        #endregion
In your code to use it:
            AutoUpdater.Start("http://website.com/Pulse_AppCast.xml");
            AutoUpdater.AutoUpdaterEvent += new AutoUpdater.AutoUpdaterEventHandler(AutoUpdater_AutoUpdaterEvent);
        /// <summary>
        /// Event handler for the AutoUpdater.   This will get if an update is available
        /// and if so, which version is available.
        /// </summary>
        /// <param name="isUpdateAvailable">TRUE = update is available.</param>
        /// <param name="currentVersion">Version that is ready to download.</param>
        /// <param name="installedVersion">Version of this application.</param>
        void AutoUpdater_AutoUpdaterEvent(bool isUpdateAvailable, Version currentVersion, Version installedVersion)
        {
            string VersionUpdateToDate = "Checking for update...";
            if (!isUpdateAvailable)
            {
                VersionUpdateToDate = string.Format("App is up to date");
            }
            else
            {
                VersionUpdateToDate = string.Format("App version {0} is available", currentVersion);
            }
     
            // Unsubscribe
            AutoUpdater.AutoUpdaterEvent -= AutoUpdater_AutoUpdaterEvent;
        }
Aug 27, 2013 at 6:31 AM
Looks like great idea. Looking forward to implement it.
Mar 16 at 1:01 PM
I am failing miserably to find the correct positions to insert above piece of code in AutoUpdater.cs.

Would somebody please point me to it? Many thanks!!

Martin
Mar 16 at 11:04 PM
Edited Mar 16 at 11:21 PM
I have found it - the first part goes at the end into BackgroundWorkerDoWork, the next part directly below that.

I even managed to translate the event handling to VBNet.

Best regards
Maritn
Mar 24 at 9:03 AM
Since I have added this part the AutoUpdate.NET dialog appears twice when the update check is made. What have I done wrong?
Mar 24 at 9:24 AM
Again I could answer my own question. You have to remove this part in AutoUpdater.cs:
            if (CurrentVersion > InstalledVersion)
            {
                args.IsUpdateAvailable = true;
                if (CheckForUpdateEvent == null)
                {
                    var thread = new Thread(ShowUI);
                    thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                }
            }

            if (CheckForUpdateEvent != null)
            {
                CheckForUpdateEvent(args);
            }
when you add:
            bool IsUpdateAvail = false;
            if (CurrentVersion > InstalledVersion)
            {
                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? System.Windows.Forms.Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();

                IsUpdateAvail = true;
            }
            else
            {
                IsUpdateAvail = false;
            }

            // Publish the event
            if (AutoUpdaterEvent != null)
            {
                AutoUpdaterEvent(IsUpdateAvail, CurrentVersion, InstalledVersion);
            }

        }