In order to show a first run dialog (a window that appears before the main application starts if certain conditions are met) I have decided to modify the Main method and do this:

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Properties.Settings.Default.FirstRun)
            {
                Application.Run(new RunOnce());
            }

            Application.Run(new Main());
        }

Is this a bad idea? It works, and with a bit of additional logic I can prevent the main application from even opening should the user cancel out of the first run dialog.

Thoughts?

Recommended Answers

All 2 Replies

I'm guessing you want a form that runs before the main form, but you still want to defer the application thread to the main forum? There are a few ways to do that; one of the simpler ones can be seen in a service configuration utility I wrote that looks something like this:

[STAThread]
public static void Main(string[] args) {
    RunApplication(args);
}

private static void RunApplication(string[] args) {
    Trace.AutoFlush = true;
    Trace.Listeners.Add(new TextWriterTraceListener(TraceFile, "Listener_" + ProductShortName + ".Config"));

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => {
        string msg = (e.ExceptionObject as Exception).ToString();
        string logMsg = "Fatal Error: Unhandled exception [" + msg + "]";
        string usrMsg = "An error was detected at load time. Refer to the trace log for details";

        Logger.TraceError(logMsg);
        Utility.FatalBox(usrMsg);

        if (Application.MessageLoop)
            Application.Exit();
        else
            Environment.Exit(-1);
    };

    var form = new ServiceConfig();

    if (!form.Login()) {
        Utility.FatalBox("Login failed.");
    } else {
        Application.Run(form);
    }
}

The main form is ServiceConfig, and it has a method that opens a modal dialog for logging in to an external application. All of this is done before the Application.Run(), so the login dialog appears first and if it fails or is canceled the main form won't show.

Kind of. What I am after is a form that tests a pre-defined SQL connection string, if the connection passes then a setting is set to true and the form will not be opened in the future so long as the connection still works. If, in the future, the connection fails, the setting is reset to false. If the false setting is found, the RunOnce dialog appears asking the user to verify the connection information and to re-test the connection.

I kind of need one application to open, adjust the connection string in the settings and then start the main application with those new settings.

Since the connection string is stored in the application settings file and has an application scope, not user scope, I can't edit the string without restarting the application. I was hoping this would fix that problem, but it actually doesn't seem to and I found a way to adjust the setting and restart the application in a different manner.

Thank you for the code example!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.