Using Invoke() to show Form from Threading.Time() thread

138 views Asked by At

I have the following:

  public static Form1 MainForm = new Form1();
    static ManualResetEvent _running = new ManualResetEvent(false);

    [MTAThread]
    private static void Main()
    {
        int startin = 60 - DateTime.Now.Second;
        var t = new System.Threading.Timer(o => Analyze(),
            null, startin*1000, 60000);

        _running.WaitOne();
    }

    public static void Analyze()
    {
        Action a = new Action(() => MainForm.ShowDialog());
        MainForm.Invoke(a);
    }

I'm having a hard time attempting to display the only form I have - MainForm.

What am I doing wrong here?

1

There are 1 answers

1
Servy On BEST ANSWER

When you call MainForm.Invoke you're telling the form to schedule the operation provided to be run from the application loop the form is running in. When you're calling that method your form has no application loop, and as such there's nothing to run the operation that you've scheduled, so it doesn't get run.

Ironically enough the operation you're trying to schedule in the application's loop is the creation of the application loop.

You'll need to start your application loop (using Application.Run), create your form, and then once you've done that you can schedule work for that application to perform.