Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible to "spin off" several GUI threads? (Not halting the system at Application.Run)
    primarykey
    data
    text
    <p><strong>My Goal</strong></p> <p>I would like to have a main processing thread (non GUI), and be able to spin off GUIs in their own background threads as needed, and having my main non GUI thread keep working. Put another way, I want my main non GUI-thread to be the owner of the GUI-thread and not vice versa. I'm not sure this is even possible with Windows Forms(?)</p> <p><strong>Background</strong></p> <p>I have a component based system in which a controller dynamically load assemblies and instantiates and run classes implementing a common <code>IComponent</code> interface with a single method <code>DoStuff()</code>.</p> <p>Which components that gets loaded is configured via a xml configuration file and by adding new assemblies containing different implementations of <code>IComponent</code>. The components provides utility functions to the main application. While the main program is doing it's thing, e.g. controlling a nuclear plant, the components might be performing utility tasks (in their own threads), e.g. cleaning the database, sending emails, printing funny jokes on the printer, what have you. What I would like, is to have one of these components be able to display a GUI, e.g. with status information for the said email sending component.</p> <p>The lifetime of the complete system looks like this</p> <ol> <li>Application starts.</li> <li>Check configuration file for components to load. Load them.</li> <li><strong>For each component, run <code>DoStuff()</code> to initialize it and make it live its own life in their own threads.</strong></li> <li>Continue to do main application-thingy king of work, forever.</li> </ol> <p>I have not yet been able to successfully perform point 3 if the component fires up a GUI in <code>DoStuff()</code>. It simply just halts until the GUI is closed. And not until the GUI is closed does the program progress to point 4.</p> <p>It would be great if these components were allowed to start up their own Windows Forms GUIs.</p> <p><strong>Problem</strong></p> <p>When a component tries to fire up a GUI in <code>DoStuff()</code> (the exact line of code is when the component runs <code>Application.Run(theForm)</code>), the component and hence our system "hangs" at the <code>Application.Run()</code> line until the GUI is closed. Well, the just fired up GUI works fine, as expected.</p> <p>Example of components. One hasn't nothing to do with GUI, whilst the second fires up a cute windows with pink fluffy bunnies in them.</p> <pre><code>public class MyComponent1: IComponent { public string DoStuff(...) { // write something to the database } } public class MyComponent2: IComponent { public void DoStuff() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form()); // I want the thread to immediately return after the GUI // is fired up, so that my main thread can continue to work. } } </code></pre> <p>I have tried this with no luck. Even when I try to fire up the GUI in it's own thread, the execution halts until the GUI as closed.</p> <pre><code>public void DoStuff() { new Thread(ThreadedInitialize).Start() } private void ThreadedInitialize() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form()); } </code></pre> <p>Is it possible to spin off a GUI and return after <code>Application.Run()</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload