Start a process after a certain time interval in C #?

I have a C # wpf application, where in the main () method I check for a specific condition, and if it is true, I start another process, however I need to start the process after a certain timeout. So for example:

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. } } 

I could use Thread.Sleep (), but this will make the current program sleep. Thus, in other words, I want the current program to terminate immediately, and then the new process starts after 5 seconds.

Thanks!

Is it possible?

+4
source share
5 answers

You have several options:

  • Change the startup process and set a delay. You can even expect the “parent finished” instead of a fixed time.
  • Scheduled Windows Tasks
  • A batch file that uses the sleep command and then runs the program
+3
source

What if the first process creates a third program. The first program will exit immediately, and the third will simply sleep for 5 seconds, and then launch the second program.

+4
source

You can use the Task Scheduler api and set up a one-time task that will launch the application in the next 5 seconds. Voice Operated Handshake: taskscheduler.codeplex.com

+1
source

You need to create a new topic. In this thread, you can use Thread.Sleep without blocking your program.

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

I can recommend a very powerful

http://quartznet.sourceforge.net/

For all your planning needs.

0
source

Source: https://habr.com/ru/post/1436436/


All Articles