user6465254
user6465254

Reputation:

C# Winforms Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead

Summary: My application starts off with a license validation form and if the client's license is valid. It should launch the main form.

However I am receiving the following error: Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.

My implementation:

static class Program
{
    static AppStartUp appStartUp_;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        appStartUp_ = new AppStartUp();
        appStartUp_.OnValidationSuccessful += OnValidationSuccessful;

        Application.Run(appStartUp_);
    }

    static void OnValidationSuccessful()
    {
        appStartUp_.OnValidationSuccessful -= OnValidationSuccessful;
        appStartUp_.Close();
        appStartUp_.Dispose();

        //Application.ExitThread();
        Application.Run(new Manager());
    }
}

Is there something that I am doing wrong?

Upvotes: 2

Views: 2284

Answers (1)

LarsTech
LarsTech

Reputation: 81620

Your first Application.Run is still running during that OnValidationSuccessful event. Assuming that event is closing the form, try setting a variable instead:

static bool appOK = false;

then in your event, set it to true:

static void OnValidationSuccessful()
{
  appOK = true;
}

then in your Main procedure, it would look like this:

Application.Run(appStartUp_);
if (appOK) {
  Application.Run(new Manager());
}

Upvotes: 3

Related Questions