tags:

views:

47

answers:

3

Hello, I am developing a plugin for an application, that application "consumes" my code (classLibrary), and executes the "Init()" method inside his own Thread. Inside the Init() I have a while(true) statement, so that my plugin can run continuously. Yesterday, I started to make a windowsForm for configuration of my plugin (using XML), and now I want to show it, but it keeps vanishing. My code is as follows:

Doing this will show the form, but, it wont re-paint because it is launched on that same thread as the while(true) is.

MaForm settingsForm = null;

void init(){
    While(true){
      if(settingsForm == null){
         settingsForm = new MaForm();
         settingsForm.show();
      }

    }
}

Version that shows, but then vanishes.

MaForm settingsForm = null;
Thread worker = null;
void init(){
    While(true){
      if(worker == null){         
         worker = new Thread(new ThreadStart(formStuff));
         worker.Start();
      }    
    }
}
void formStuff()
{
     if(settingsForm == null){
         settingsForm = new MaForm();
         settingsForm.show();
      }
}

What am I doing wrong? Is there something about Threading I am missing? What do you guys think?

+1  A: 

The thread is starting, showing your form, then finishing up and shutting down (which closes the form).

Showing a form on a separate thread is nearly always problematic. Forms require a message pump to be running - so they typically will only work properly if they're started and run on the GUI thread.

One option would be to invoke the function to show your form onto your main thread. This will make your form load (and run) on the main thread.

Alternatively, you can start an entire message pump on the form's thread, and set the thread to STA (this is important). However, I suggest avoiding this approach if possible.

Reed Copsey
This is a plugin, my main method is init() (only one i have direct access to), and running my code there, shows the form, but it nothing inside it (you know, when you create a while(true) function inside a button.click call without threading).
SilentWarrior
It doesn't really matter whether it's a plugin or not. Everything I wrote is true, there. Having "while(true)" is usually a good thing to avoid - especially if you're not doing anything there. You should be providing your plugin ~something~ it can use to work with the UI thread, if your plugin will be working with user interfaces - if nothing else, pass init an ISynchronizeInvoke argument (your main form), so it can synchronize back to the UI thread. See: http://msdn.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.aspx
Reed Copsey
A: 

A good way of dealing with threading issues in C# is to comment out "using System.Threading;" at the top of your classes and forms. You may have some compelling reason for showing forms with a Thread, but probably not, since Form.Show() is not a blocking call.

Update: if you're trying to show a form from your Main() method, try using ShowDialog() instead. This call will block until the form is closed.

MusiGenesis
A: 

you can try this: create the form, enter the infinite loop, call DoEvents() so that your form can process windows messages:

if(settingsForm == null){
   settingsForm = new MaForm();
   settingsForm.show();
}


while (settingsForm != null && settingsForm.Visible)
{
    System.Windows.Forms.Application.DoEvents();
}

EDIT: may be you can replace the 'true' condition by a check on the settingsForm visibility. When the form is closed, it's a waste to stay in an infinite loop.

najmeddine
can you tell me why the down vote, I'm here also to learn. If DoEvents looks evil, it's the same thing when, in a WinForms application, we call Application.Run();
najmeddine
I downvoted because under normal circumstances you shouldn't ever have to use DoEvents. Creating a form and showing it is the ultimate "normal circumstance" in a WinForms app, and doesn't require threading at all, and definitely doesn't require an infinite loop with a DoEvents call in the middle. Also, I'm not sure your code would fix his problem anyway (haven't tried it), but even if it did it would be inadvisable to use it.
MusiGenesis
Nothing personal, though, and your sailboat avatar makes me mildly happy. :)
MusiGenesis