Windows Develop Bookmark and Share   
 index > Windows Forms General > Force application exit from ApplicationContext constructor
 

Force application exit from ApplicationContext constructor

Hello.

I have a tray only application.

In the constructor of the ApplicationContext derived class, in some cases (process already running), I have to close the application.

Can't find any way to do this.

ApplicationContext.ExitThread does not work.

Application.Exit neither.

Any idea?

 

class Client : ApplicationContext

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] a_sArgs)

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Client());

}

/// <summary>

/// Tray icon.

/// </summary>

NotifyIcon m_niTrayIcon = new NotifyIcon();

/// <summary>

///

/// </summary>

Client()

{

if (...my exit condition...)

{ // Stop�

ExitThread(); // Does not work.

return;

}

m_niTrayIcon.Icon = new Icon(...);

m_niTrayIcon.Visible = true;

}

}

sylde.net  Wednesday, December 20, 2006 12:14 PM
That's too early. By the time the constructor runs, the message loop (Application.Run()) is not yet active. You could throw an exception in the constructor that you catch in Main().  Or a bit more cleanly: check the condition before calling Run().
nobugz  Wednesday, December 20, 2006 12:39 PM
That's too early. By the time the constructor runs, the message loop (Application.Run()) is not yet active. You could throw an exception in the constructor that you catch in Main().  Or a bit more cleanly: check the condition before calling Run().
nobugz  Wednesday, December 20, 2006 12:39 PM

nobugz wrote:
That's too early. By the time the constructor runs, the message loop (Application.Run()) is not yet active.

That's it! Thanks...

sylde.net  Wednesday, December 20, 2006 1:42 PM

You can use google to search for other answers

Custom Search

More Threads

• Hiding the clicked button
• Disable FileName in SaveFileDialog
• Higlighted item in ComboBox
• VB.NET vs AS/400
• Committing ToolStripComboBox
• RichTextBox memory leak
• Close active window on click event
• How to make a description appear when cursor is over a button?
• Capture Inbound/Outbound traffic
• Detecting if preview mode