How to run and shutdown programs by timer?

Hi there!

I want to create an application A in C# that automatically starts another application B on a specified time and let it run for xx minutes. When that time has passed the application B should automatically be stopped.

I don't want to do this by creating a windows service since i think there are already way too many of those running :)

Any help would be appreciated.

Peregrin



Answer this question

How to run and shutdown programs by timer?

  • duck thing

    You can control other applications with the Process class:


    using System.Diagnostics;
    using System.Threading;

    ...

    Process process = Process.Start("ApplicationB.exe");
    Thread.CurrentThread.Sleep(60000); // Wait 1 minute
    process.CloseMainWindow();
    process.Close();



  • Jassim Rahma

    thank you all for your replies, they really helped me alot!
  • Ariel Valentin

    If you want to do some job after some number of minutes then you should drop a timer control on your main app form. Set the timer Interval to 60000 = 1minute. When you click the start button that will activate aplication B then start the timer and strore that moment datetime value in a variable. On every timer tick check if DateTime.Now is bigger then datetime variable value + xx minutes. If it is then stop your application.
    Starting Your app will be something like
    Process AppB = new Process();
    AppB.FileName = "full path to app exe";
    AppB.Start();

    Now when the time for closing AppB hass passed you should execute
    AppB.Kill();



  • Amjath

    See Process Class in System.Diagnostics, This will help you to run and stop application. You can use timer(s) to check when a specfic application has to be run and when to stop and use the Process class's function like Start and Kill to start and stop the application.

    When an application starts you have to associate a seprate timer with to to pass some value that agyer how many seconds it'll stop and put that time in Interval vaue after converting that value to mili seconds. Handle the Tick Event of timer and in Tick code Kill that application process using Kill Method of process class.

    Also see ProcessInfo for more options and to have referece to some running process. This is recomended at this situation becuase you need to know which application or process is runnig so u'll have a reference to it when you have to kill it because you created it when you wanted to start the application.

    I hope you have understood the idea and you can follow it!

    Best Regards,



  • How to run and shutdown programs by timer?