【问题标题】:calling public method on windows form after application.run在 application.run 之后在 windows 窗体上调用公共方法
【发布时间】:2012-05-24 21:20:57
【问题描述】:

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

【问题讨论】:

  • 如果您的RunImport 方法不需要访问用户界面,那么您应该可以在Application.Run 之前调用RunImport

标签: c# winforms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 2010-12-24
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    相关资源
    最近更新 更多