繁体   English   中英

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

[英]calling public method on windows form after application.run

我有一个通常作为计划任务运行的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());
    }
}

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();
}

暂无
暂无

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

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