I had a similar problem in the application I wrote. My main user interface was a form working on a main theme. I had a help dialogue that I wanted to launch as a modeless dialogue. This was easy to implement, even in terms of ensuring that I only have one instance of the help dialog. Unfortunately, any modal dialogs that I used made the help dialog also lose focus - when it was, when some of these modal dialogs were executed, which would be most useful using the help dialog.
Using the ideas mentioned here and elsewhere, I managed to overcome this error.
I declared a thread inside my main interface.
Thread helpThread;
The following code is about an event that opens in the help dialog box.
private void Help(object sender, EventArgs e) { //if help dialog is still open then thread is still running //if not, we need to recreate the thread and start it again if (helpThread.ThreadState != ThreadState.Running) { helpThread = new Thread(new ThreadStart(startHelpThread)); helpThread.SetApartmentState(ApartmentState.STA); helpThread.Start(); } } void startHelpThread() { using (HelpDialog newHelp = new HelpDialog(resources)) { newHelp.ShowDialog(); } }
I also needed to initialize the thread added to my constructor to make sure that I did not reference the null object when I first ran this code.
public MainWindow() { ... helpThread = new Thread(new ThreadStart(startHelpThread)); helpThread.SetApartmentState(ApartmentState.STA); ... }
This ensures that the thread has only one instance at any given time. The thread itself starts the dialog and stops after the dialog closes. Since it works in a separate thread, creating a modal dialog from the main user interface does not cause the help dialog to freeze. I needed to add
helpDialog.Abort();
to the closing event of the form of my main user interface to make sure that the help dialog closes when the application is completed.
Now I have a modeless help dialog that is not affected by any modal dialogs created from my main user interface, and this is exactly what I wanted. This is safe because there is no connection between the main interface and the help dialog.
Elgar Storm Mar 29 2018-12-12T00: 00Z
source share