在application.run之后在windows窗体上调用public方法

geo*_*rtz 5 c# winforms

我有一个通常作为计划任务运行的Windows窗体,所以我的想法是我会在任务中传入命令参数以使其自动运行.这样我可以在没有参数的情况下在本地运行它,以便在必要时手动运行它 但是我不太确定如何在它作为任务运行时调用Application.Run时调用新表单的方法.现在它只是显示表单并退出那里而不是继续到i.RunImport()行.有任何想法吗?这是我的代码.谢谢.

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length > 0)
    {
        if (args.Any(x => x == "run=1"))
        {
            var i = new Importer();
            Application.Run(i);
            i.RunImport();
        }
    }
    else
    {
        Application.Run(new Importer());
    }
}
Run Code Online (Sandbox Code Playgroud)

IAb*_*act 7

为事件编写事件处理程序Form.Load:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length > 0)
    {
        if (args.Any(x => x == "run=1"))
        {
            var i = new Importer();
            // modify here
            i.Load += ImporterLoaded;
            Application.Run(i);

            // unsubscribe
            i.Load -= ImporterLoaded;
      }
    }
    else
    {
        Application.Run(new Importer());
    }
}

static void ImporterLoaded(object sender, EventArgs){
   (sender as Importer).RunImport();
}
Run Code Online (Sandbox Code Playgroud)