C# uses Mutex to realize form switching [C# form switching]

How to achieve multi-window switching? How can I do it if I don't want to use hide and show handling?

Use a case as an example to help understanding: set an idea:

1 Execution: When logging in, first determine whether there is a database link configured and ensure normal access;

   Result: There is a configuration file and the normal access database can be accessed and return true;

               If the configuration file does not exist, or the database link information in the configuration file is invalid, return flase;

2 Execute: Receive the result of the first step, if the judgment result in the first step returns No

   Result: Enter the database configuration form, only the configuration is correct to enter the third window

3 Execute: If the configuration of the second step is correct and the saving is completed, the second database configuration window will be closed and the system login interface will be entered.

   Result: Whether the returned user information to be logged in is correct

4 Execution: The form of the third step is ShowDialog()==DialogResult.OK, then switch to the main form of the system menu function interface.

 This involves three forms: To successfully log in and enter the main form Main, you need to go through two steps: 1. Determine the database link 2. Login How to realize the switch?


Use Mutex to switch the form, the code is as follows:

using MedicalRecord.Class;
using System;
using System.Threading;
using System.Windows.Forms;

namespace MedicalRecord
{
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            bool ret;
            Mutex mutex = new Mutex(true, Application.ProductName, out ret);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            CheckConfig checkConfig = new CheckConfig();

            if (ret)
            {
                //To judge the database configuration, the focus here is to introduce the use of mutex to achieve window switching, so the judgment is no longer verbose
                string AppSettingString = checkConfig.GetAppSettings();
                bool checkresult = checkConfig.TestConntion(AppSettingString);

                //Receive the judgment result, if you need to reconfigure, execute the code in if: run the SetConfig form
                if (checkresult == false)
                {
                    Application.Run(new SetConfig());
                    //When the database link is configured and the form is closed, the mutex is released once
                    mutex.ReleaseMutex();
                }

                try
                {
                    //After configuration, enter the login form and instantiate the login form
                    Form_login login = new Form_login();

                    if (login.ShowDialog() == DialogResult.OK)
                    {
                        //If the login is successful, you need to enter the main main form
                        Application.Run(new Main());
                    }
                    mutex.ReleaseMutex();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(null, ex.ToString(), Application.ProductName,
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Application.Exit();
                }
            }
        }
    }
}
Then, all the forms need to close: Application.Exit();

This is a personal idea of ​​mine. The most beginners make everyone laugh. I come up with my own ideas and let everyone give pointers. I hope to find a better way with everyone's pointers, thank you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723616&siteId=291194637