• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

Raising an event using C#

I have this subroutine that checks a manifest file if there are updates on my server. I do not want it to give the user an option whether to download or not. I want it to download automatically and notify the user after the download possibly by raising an event. I do not want a message box. How can I achieve this with raising an event. Here is my code below.
I also want to raise an event if there are no updates and not a message box.

Thanks.


private static void UpdateApplication()
            {
                  ApplicationUpdaterManager updaterManager = ApplicationUpdaterManager.GetUpdater( config.ApplicationID );
                  Manifest[] manifests = updaterManager.CheckForUpdates( config.ManifestUri );
                  
                  if(manifests.Length > 0)
                  {
                        if (DialogResult.Yes == MessageBox.Show(null,"Updates available do you want to apply?","SimpleAppStart",MessageBoxButtons.YesNo,MessageBoxIcon.Question) )
                        {
                              foreach(Manifest manifest in manifests)
                              {
                                    manifest.Application.Location = Path.GetDirectoryName(config.ExecutablePath);
                                    manifest.Apply = true;
                              }
                              updaterManager.Download( manifests, TimeSpan.MaxValue );
                              updaterManager.Activate( manifests );
                        }
                  }
                  else
                  {
                        MessageBox.Show(null,"No updates available","SimpleAppStart",MessageBoxButtons.OK,MessageBoxIcon.Information);
                  }
            }
0
Sirdots
Asked:
Sirdots
  • 2
1 Solution
 
wishguitarCommented:
//Declare your events
private static event UpdatesReceivedEventHandler UpdatesReceived;
private static event EventHandler NoUpdatesReceived;

private static void UpdateApplication()
{
      ApplicationUpdaterManager updaterManager = ApplicationUpdaterManager.GetUpdater( config.ApplicationID );
        Manifest[] manifests = updaterManager.CheckForUpdates( config.ManifestUri );
             
      if(manifests.Length > 0)
      {
                //Raise Event
            if (UpdatesReceived != null)
                  UpdatesReceived(updaterManager,new UpdatesReceivedEventArgs(manifests));
      }
      else
      {
                //Raise Event
            if (NoUpdatesReceived != null)
                  NoUpdatesReceived(null,EventArgs.Empty);
      }
}

//Declare custom delegate to pass data needed
public delegate void UpdatesReceivedEventHandler(ApplicationUpdaterManager updater, UpdatesReceivedEventArgs e);
//Declare custom event args for custom data
public class UpdatesReceivedEventArgs : EventArgs
{
      public UpdatesReceivedEventArgs(Manifest [] manifests) : base()
      {
            _manifests = manifests;
      }
      
        private Manifest [] _manifests;
      public      Manifest [] Manifest
      {
            get { return _manifests; }
      }
}
0
 
SirdotsAuthor Commented:
Thanks wishquitar. So where will this show when I do this. I still want to be able to convey a message to my users maybe on a statusbar or something to show that there was an update or there was no update. I just want to get rid of the message box. Can I get a little more explanation about this.

Thanks.
0
 
wishguitarCommented:
I don't know the name of you class, so I'll use YourClass as my class name.

In order to receive event notifications, you need to register for the event, which is called adding a handler.  Something like this:

//Add handlers
YourClass.UpdatesReceived += new UpdatesReceivedEventHandler(YourClass_UpdatesReceived);
YourClass.NoUpdatesReceived += new EventHandler(YourClass_NoUpdatesReceived);

The parameter for the new event handlers is the method which will handle these event notifications, for example...

private static void YourClass_UpdatesReceived(ApplicationUpdaterManager updater, UpdatesReceivedEventArgs e)
{
       Manifest [] manifests = e.Manifests;

      foreach(Manifest manifest in manifests)
     {
           manifest.Application.Location = Path.GetDirectoryName(config.ExecutablePath);
           manifest.Apply = true;
      }
      updaterManager.Download( manifests, TimeSpan.MaxValue );
      updaterManager.Activate( manifests );
     
     //Write to a status bar or something letting user know updates where done.

}


Hope this helps...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now