Robostich
Robostich

Reputation: 41

Problem with the run of several applications - System.InvalidOperationException

I am currently in the development of a windows forms application and I am having a little problem that is blocking me completely.

My problem: In the program.cs file I have the execution of a first form which allows me to check if the person is well connected to the Internet. Once the verification is done, I would like to close this form and open my application, I tried with Application.Run(new App());, but it tells me that I cannot do two starts in a thread.

// program.cs

using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;
using System.IO;
using Newtonsoft.Json;

namespace MyApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Loading());
        }

        public static bool CheckInternet()
        {
            try {
                using (var web = new WebClient())
                using (web.OpenRead("http://google.com"))
                    return true;
            } catch {
                return false;
            }
        }
    }
}
// loading.cs

using System;
using System.Windows.Forms;

namespace MyApp
{
    public partial class Loading : Form
    {
        public Loading()
        {
            InitializeComponent();

            this.text_version.Text = "0.0.0.1";

            if (Program.CheckInternet())
            {
                var timer = new System.Windows.Forms.Timer();
                timer.Interval = 2000;

                timer.Tick += (s, e) =>
                {
                    timer.Stop();
                    StartApp();
                };

                timer.Start();
            }
        }

        private void StartApp()
        {
            this.l_info.Text = "Starting app...";
            this.l_info.Location = new System.Drawing.Point(70, 270);

            Application.Exit();

            Application.Run(new App()); // Error here
        }
    }
}

I hope explain it well. If you have any questions do not hesitate.

Thanks to anyone who will have the time to help me.

Upvotes: 1

Views: 334

Answers (1)

Brett Caswell
Brett Caswell

Reputation: 1504

There are various implementations you could do here for control flow (I think @jimi's comments on using DialogResult is a decent approach), but what's important is that you should not attempt to do a nested Application.Run call.

In this implementation, we'll close Loading Form which will continue the execution in Main. Note that doing Application.Exit also will continue execution in Main, but it won't actually Run any new forms. As such, you do have a scenario where we can add that behavior (whether or not you actually intended to have this behavior is up to your requirements).

[STAThread]
static void Main()
{
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Application.Run(new Loading());
   Application.Run(new App());
}

Next,

we'll move Application.Exit() to the else-block of Program.CheckInternet conditional check.

we'll remove Application.Run from StartApp

we'll do Close() the Loading Form in the now improperly named StartApp function scope.

public Loading()
{
    InitializeComponent();
    this.text_version.Text = "0.0.0.1";

    if (Program.CheckInternet())
    {
        var timer = new System.Windows.Forms.Timer();
        timer.Interval = 2000;
        timer.Tick += (s, e) =>
        {
            timer.Stop();
            StartApp();
        };

        timer.Start();
    } else {
        Application.Exit();
    }

}

private void StartApp()
{
    this.l_info.Text = "Starting app...";
    this.l_info.Location = new System.Drawing.Point(70, 270);

    Close(); //this.Close for Form instance close, not Application.Exit
}

Upvotes: 1

Related Questions