Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I added another form to my system tray application and in that form the user can perform an action that requires the program to reload itself

 

// Alert the user that the application will restart
MessageBox.Show("Settings Saved. Click OK to restart the application", 
                        "Saved Settings", MessageBoxButtons.OK,
                        MessageBoxIcon.Stop);

// Shut down the current app instance
Application.Exit();

// Restart the app
System.Diagnostics.Process.Start(Application.ExecutablePath);

 

Problem is, I can't get the icon to disappear since the application is being exited from a form other than the main one that loaded the icon. If I exit the application from the main form, icon gets removed from the tray no problem.

 

How can I get rid of the icon when I exit the app from another form instance?

  • Leaders
Posted

Why can't you close the main window?

 

CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

[/Quote]

 

Since Application.Exit appearently does not close forms in a normal fasion, it is not surprising that the NotiftyIcon is not properly taken care of.

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted (edited)

What I generally do is use a static property like so:

Public Class Form1
   Static _Inst As Form1

   Public Sub New()
       'ADD this to the existing constructor code
       'Store a static reference to the main form
       _Inst = Me
   End Sub

   Public Shared ReadOnly Property AppInstance() As Form1
       Get
           'Return the main form
           Return _Inst
       End Get
   End Function
End Class

'To close the application:
Form1.AppInstance.Close()

 


public class Form1 : System.Windows.Forms.Form {
static Form1 _inst;

public Form1() {
// ADD this to the constructor
_inst = this;
}

public static Form1 AppInstance {
get {
return _inst;
Form1.AppInstance.Close();
}
}
}

// To close the app,
[/Code]

Edited by snarfblam
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...