5

通常はスケジュールされたタスクとして実行される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());
    }
}
4

1 に答える 1

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();
}
于 2012-05-24T13:20:57.320 に答える