views:

59

answers:

1

Im developing a plugin for Rhino, and when i run the command starting plugin i carries out the following. It creates a splash form, which has a timer on it, and after 2sec i loads another form.

If i by mistake click the plugin-icon again, it creates another instance of the spash form, which loads the plugin again.

How do i prevent this?

This is the code that makes the form.

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
            {

                Splash Splash = new Splash();
                Splash.Show();

                return IRhinoCommand.result.success;
            }
+3  A: 
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
    if (!Application.OpenForms.OfType<Splash>().Any())
    {
        new Thread(() => Application.Run(new Splash())).Start();
    }
    return IRhinoCommand.result.success;
}
Ani
When i try to use it through System.Windows.Forms it can't find the OfType property, how do i use it?
Bildsoe
@Bildsoe: If you are on .NET 3.5 or later, make sure you have `System.Core` referenced and a `using System.Linq;` directive at the top of the file. If you are not, loop through `Application.OpenForms` and check if any form is a `Splash` object.
Ani
How do i do the Splash.show, when it is running as a thread?
Bildsoe
@Bildsoe: The code starts a message-loop for the form on a separate thread and then displays it.
Ani
Ok, i just tried the code, and the forms is shown and then shortly after it disappears.
Bildsoe
@Bildsoe: Ok, just call `Application.Run(new Splash());` without the thread. It will block until the form is closed. Is that what you require?
Ani
Is there any reason for doing the Application.Run instead of simply writing the following inside the if:Splash Splash = new Splash();Splash.Show();
Bildsoe
@Bildsoe: There needs to be a message-loop. `Form.Show()` will return immediately.
Ani
Ah ok, thanks. I'll see if i can make it work.
Bildsoe