简体   繁体   English

应用程序重新启动后如何运行命令

[英]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. 例如,当用户单击他想要的语言时,该语言将被检查为true,而忽略另一个被检查为= 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. 首先,我将选择的语言保存在app.config文件中,您可以在启动时检查配置并检查适当的语言。

Second for restarting the application I would use one of two options: 其次,重新启动应用程序,我将使用以下两个选项之一:

1) Application.Restart() 1)Application.Restart()

2) Start a second application and then end the first. 2)启动第二个应用程序,然后结束第一个应用程序。 See this post: Restart WinForms Application 看到这篇文章: 重新启动WinForms应用程序

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. 重新启动SAME应用程序控件内的应用程序后,您正在检查语言。 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. 这就是我可以给你的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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