orbwave Posted June 3, 2005 Posted June 3, 2005 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? Quote
Leaders snarfblam Posted June 3, 2005 Leaders Posted June 3, 2005 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. Quote [sIGPIC]e[/sIGPIC]
orbwave Posted June 3, 2005 Author Posted June 3, 2005 I've tried that, but I can't figure out how to get a reference to the main form to call the Close() method. Quote
Administrators PlausiblyDamp Posted June 3, 2005 Administrators Posted June 3, 2005 Although the code is VB the ideas in http://www.xtremedotnettalk.com/showthread.php?t=83092 should be what you are after. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted June 3, 2005 Leaders Posted June 3, 2005 (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 June 3, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
orbwave Posted June 3, 2005 Author Posted June 3, 2005 Sweet, easily converted to C# and works like a charm. Thanks a million! Although the code is VB the ideas in http://www.xtremedotnettalk.com/showthread.php?t=83092 should be what you are after. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.