How to shut down a second UI thread

Alain

I need to be able to start up a window on a second UI thread and shut it down again at will.

This is my current code:

/// <summary>Show or hide the simulation status window on its own thread.</summary>
private void toggleSimulationStatusWindow(bool show)
{
    if (show)
    {
        if (statusMonitorThread != null) return;
        statusMonitorThread = new System.Threading.Thread(delegate()
        {
            Application.Run(new AnalysisStatusWindow(ExcelApi.analyisStatusMonitor));
        });
        statusMonitorThread.Start();
    }
    else
    {
        if (statusMonitorThread != null) 
            statusMonitorThread.Abort();
        statusMonitorThread = null;
    }
}

AnalysisStatusWindow is a fairly basic System.Windows.Forms.Form

The above code is successfully creating the new UI thread, but my request to Abort the thread is ignored. The result is that toggling the above function multiple times simply results in new windows opening up - all of which are on their own thread and fully functional.

Is there any way I can pass a message to this thread to shut down nicely? Failing that, is there any way to make sure Abort() really kills my second UI thread?


I've tried using new Form().Show() and .ShowDialog() instead of Application.Run(new Form()), but they aren't any easier to shut down.

If anyone is questioning the need for a separate UI thread, this code exists in an Excel Add-in, and I cannot control the fact that the Excel UI blocks while calculations for a given cell are underway. For that reason, when a long running custom formula executes, I require this second UI thread to display progress updates.

Alain

Thanks to Hans for his comment. I solved my problem using the following code:

/// <summary>Show or hide the simulation status window on its own thread.</summary>
private void toggleSimulationStatusWindow(bool show)
{
    if (show)
    {
        if (statusMonitorThread != null) return;
        statusMonitorWindow = new AnalysisStatusWindow(ExcelApi.analyisStatusMonitor);
        statusMonitorThread = new System.Threading.Thread(delegate()
        {
            Application.Run(statusMonitorWindow);
        });
        statusMonitorThread.Start();
    }
    else if (statusMonitorThread != null)
    {
        statusMonitorWindow.BeginInvoke((MethodInvoker)delegate { statusMonitorWindow.Close(); });
        statusMonitorThread.Join();
        statusMonitorThread = null;
        statusMonitorWindow = null;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I shut down a Buildbot master from the web UI?

From Dev

How to shut down X?

From Dev

How to shut down Javafx?

From Dev

Sending shut down signal to another thread in Java

From Dev

How to shut down openconnect cleanly?

From Dev

How can I gracefully shut down RawCap?

From Java

How to stop/shut down an elasticsearch node?

From Dev

How to shut down rethinkdb running on Mac OSX

From Dev

How to shut down a Python TCPServer or HTTPServer or SimpleHTTPServer?

From Dev

How to shut down Android phone, programmatically?

From Dev

How can I shut down Rserve gracefully?

From Dev

How to properly shut down POE and POE::Session?

From Dev

How to force all sounds to shut down

From Dev

How to shut down application after BeanCreationException is thrown

From Dev

How to automatically start and shut down VirtualBox machines?

From Dev

How to force shut down a hibernated Virtual PC?

From Dev

how to shut down a server from the webinterface

From Dev

How to shut down a Python TCPServer or HTTPServer or SimpleHTTPServer?

From Dev

How to Shut down Ubuntu by using keyboard?

From Dev

How to shut down Fedora without logging in

From Dev

How does Linux operating system shut down?

From Dev

How to correctly shut down a JUnit-Runner?

From Dev

How to make a program shut itself down

From Dev

How to shut down computer immediately in Win 10

From Dev

How to properly shut down a tcp socket connection in spring?

From Dev

How do I cleanly shut down a mesos-slave?

From Dev

How to capture exiting of Qt Application when shut down by debugger?

From Dev

How can I cleanly shut down a RabbitTemplate in Spring Rabbit?

From Dev

How do I shut down executor when using it with CompletableFuture

Related Related

HotTag

Archive