简体   繁体   中英

How to run a command after application restarted

I am trying to restart my application and run a command. For example, when the user clicks the language he wants it goes checked true and ignores the other one with checked = false. When that is done, the application restarts and checks what language the users checked after the restart and gets the language.

   public Application()
    {
        InitializeComponent();

        check_language();
        languages();
    }

    private void lang_english_Click(object sender, EventArgs e)
    {

        // problem *******

        Application.Restart();

        // if i remove this is works ok.
        // when app is restarted it is like starting it so i dont think
        // this works at all. is there an other way to read this?
        // maybe with a bool?

        lang_english.Checked = true;
        //Ignore
        lang_portuguese.Checked = false;

        MessageBox.Show("Language was set to English.\r\nCliente will now restart.", "Language", MessageBoxButtons.OK, MessageBoxIcon.Information);
        check_language();
    }

    private void lang_portuguese_Click(object sender, EventArgs e)
    {
        lang_portuguese.Checked = true;
        //Ignore
        lang_english.Checked = false;

        MessageBox.Show("Language was set to Portuguese.\r\nCliente will now restart.", "Language", MessageBoxButtons.OK, MessageBoxIcon.Information);
        check_language();
    }

    private void languages()
    {
        //Languages
        }
    }

    private void check_language()
    {
        if (lang_english.Checked == true)
        {
            languages(); //Get the languages

            //Ignore
            lang_portuguese.Checked = false;
        }
        else if (lang_portuguese.Checked == true)
        {
            languages(); //Get the languages

            //Ignore
            lang_english.Checked = false;
        }
    }

First I would save the selected language in the app.config file, the you can check the config on start-up and check the appropriate language.

Second for restarting the application I would use one of two options:

1) Application.Restart()

2) Start a second application and then end the first. See this post: Restart WinForms Application

You may also want to look into changing the language at run-time Change language at run-time

You are checking your language AFTER restarting the application inside the SAME application controls. You should write and read the language in a file so you can check it in there:

private void lang_portuguese_Click(object sender, EventArgs e)
{
new System.IO.StreamWriter(new System.IO.FileStream("File.ext", System.IO.FileMode.Create)).Write("Portuguese");
}

private void check_language()
{
String lang = new System.IO.StreamReader("YouFile.ext").ReadLine();
    if (lang == "English")
    {
        languages(); //Get the languages

        //Ignore
        lang_portuguese.Checked = false;
    }
    else if (lang == "Portuguese")
    {
        languages(); //Get the languages

        //Ignore
        lang_english.Checked = false;
    }
}

This are EXAMPLES. you should write and read with validation and creating instances of your stream, so you close it after finishing Reading/writting. This is the idea I can give you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM