Nicke Manarin
Nicke Manarin

Reputation: 3358

Cross Thread call inside Console Application (close the console from another thread)

I have a notifyIcon inside a thread, the icon have a contextMenu with a "Close" option, but I'm not able to close the console from there...

Using this code only closes the notifyIcon, not the console:

    public void mnuExit_Click(object sender, EventArgs e)
    {
        notificationIcon.Dispose();
        notifyThread.Abort();
        timerThread.Abort();
        Application.Exit();
    }

I tried this, but there is no "Invoke" inside the console.

        this.Invoke((Action)delegate 
        {
            Application.Exit();
        });

Edit

I'm using a thread to create a notifyIcon in the systemTray because thats the only way, there is nothing else. The notifyIcon have a option called "Exit". The Exit event method only closes the notifyIcon, but I want to close the Console window too.

Even if I just let the Application.Exit(); inside the mnuExit_Click, only the notifyIcon closes.

My program.cs have this line of code to mantain the console open:

System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

Maybe this is the problem... not sure. (but I need this)

Upvotes: 1

Views: 635

Answers (1)

Mario Stoilov
Mario Stoilov

Reputation: 3447

Why don't you try to define your own event. You can subscribe to it in the main thread and when it is fired you can close the app in your event handler

Upvotes: 1

Related Questions