【问题标题】:Application.Restart() with CommandLine Arguments带有命令行参数的 Application.Restart()
【发布时间】:2023-04-03 17:51:01
【问题描述】:

我有一个获取命令行参数的应用程序。一段时间后,我想重新启动我的应用程序,将与第一次启动应用程序时相同的命令行参数传递给它。

private void frmSetTime_Load(object sender, EventArgs e)
{
    try
    {
        string[] cmds = System.Environment.GetCommandLineArgs();
        //Here i gets Command Line Arguments
    }
    catch (Exception ex)
    {
        MessageBox.show(ex.message);
    }
    finally
    {
        GC.Collect();
    }
}

public void ExecuteLogic(Object obj)
{
    try
    {
        //My set of Statements
        Therad.sleep(5000);
        ExecuteLogic(obj);
    }
    catch (Exception ex)
    {
        MessageBox.show(ex.message);
    }
    finally
    {
        GC.Collect();
        ApplicationRestart();
    }
}

private void ApplicationRestart()
{
    try
    {
        if (Process.GetCurrentProcess().WorkingSet64 >= 10000000)
        {                         
            Application.Restart();                    
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.message);
    }
}

【问题讨论】:

    标签: c# application-restart


    【解决方案1】:

    这将自动发生。您无需更改任何内容。

    来自Application.Restart的文档:

    如果您的应用程序在首次执行时最初提供了命令行选项,Restart 将使用相同的选项再次启动应用程序。

    【讨论】:

    • 我有一个 Form Closed 事件也有影响,因为我的应用程序退出但没有重新启动。
    • 大家好,我得到了解决方案。它适用于 Form_closed 事件我只需添加 private void ApplicationRestart() { try { if (Process.GetCurrentProcess().WorkingSet64 >= 10000000) { Form1.FormClosed -= new FormClosedEventHandler(Form1_FormClosed);应用程序.Restart(); } } catch (Exception ex) { MessageBox.Show(ex.message); } } 另外不要忘记在 Form_closed 事件中终止进程。感谢您的帮助。
    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2020-11-08
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多