4

我有 ac# wpf 应用程序,在 main() 方法中,我检查某个条件,如果为真,我运行另一个进程,但是,我需要在某个超时后启动该进程。因此,例如:

override OnStartUp()

    {
           if(condition == true)
           {
                ProcessStartInfo p = new ProcessStartInfo("filePath");
                p.Start(); // This should wait for like say 5 seconds and then start.
                  return;   // This will exit the current program.
            }
    }

我可以使用 Thread.Sleep() 但这也会导致当前程序进入睡眠状态。所以,换句话说,我希望当前程序立即终止,然后新进程在 5 秒后启动。

谢谢!

这可能吗?

4

5 回答 5

4

如果第一个进程创建了第三个程序会怎样。第一个程序立即退出,而第三个程序将简单地休眠 5 秒钟,然后将启动您的第二个程序。

于 2012-09-26T18:04:03.747 回答
3

你有几个选择:

  1. 修改正在启动的进程并将延迟放在那里。您甚至可以等待“父母已结束”而不是固定时间。
  2. Windows 内置计划任务
  3. 使用 sleep 命令然后运行程序的批处理文件
于 2012-09-26T18:03:52.083 回答
1

您可以使用 Task Scheduler api 并设置一个时间任务,该任务将在接下来的 5 秒后启动应用程序。Nice 托管包装器:taskscheduler.codeplex.com

于 2012-09-26T18:18:55.873 回答
0

您需要创建一个新线程。在这个线程中,您可以使用 Thread.Sleep 而不会阻塞您的程序。

public class MyThread
{
   public static void DoIt()
   {
      Thread.Sleep(100);
      // DO what you need here
   }
}
override OnStartUp()
{
    if(condition == true)
    {
       ThreadStart myThread = new MyThread(wt.DoIt);
       Thread myThread = new Thread(myThread);
       myThread.Start();
    }
}
于 2012-09-26T18:05:48.580 回答
0

我可以推荐非常强大的

http://quartznet.sourceforge.net/

满足您的所有日程安排需求。

于 2012-09-26T18:06:49.533 回答