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;

}

}



Answer this question

Force application exit from ApplicationContext constructor

  • cybertaz69

    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().


  • AnaC

    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...


  • Force application exit from ApplicationContext constructor