locked
How do you launch these processes at different time intervals? RRS feed

  • Question

  • I'm in the process of creating an EXE that constantly runs (infinite while loop) and I want it to launch various process all at different time intervals. Some every 30 seconds.... some every 30 minutes, etc.... Here is my code so far. Let me know if you have any ideas.

    namespace Powershell
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
    // Process 1
    ProcessStartInfo ServiceMon = new ProcessStartInfo();
    ServiceMon.FileName = @"powershell.exe";
    ServiceMon.Arguments = @"& 'C:\Scripts\Monitoring\ServiceMonitoring.ps1'";
    ServiceMon.RedirectStandardOutput = true;
    ServiceMon.RedirectStandardError = true;
    ServiceMon.UseShellExecute = false;
    ServiceMon.CreateNoWindow = true;
    Process ServiceMonProc = new Process();
    ServiceMonProc.StartInfo = ServiceMon;
    ServiceMonProc.Start();
    
    // Process 2
    ProcessStartInfo RAMMonitoring = new ProcessStartInfo();
    RAMMonitoring.FileName = @"powershell.exe";
    RAMMonitoring.Arguments = @"& 'C:\Scripts\Monitoring\RAMMonitoring.ps1'";
    RAMMonitoring.RedirectStandardOutput = true;
    RAMMonitoring.RedirectStandardError = true;
    RAMMonitoring.UseShellExecute = false;
    RAMMonitoring.CreateNoWindow = true;
    Process RAMMonitoringProc = new Process();
    RAMMonitoringProc.StartInfo = RAMMonitoring;
    RAMMonitoringProc.Start();
    	     }
            }
        }
    }


    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Tuesday, June 18, 2013 7:05 PM

Answers

  • Hello zperryz,

    Please use threads. I included the code below. Note that 1000 millisecond = 1 second.

    Include:

    using System.Threading;
    Then here is your code (30000 milli = 30 secs, 1800000 milli = 30 mins):
    static int interval1 = 30000;
    static int interval2 = 1800000;
    
    static void Main(string[] args)
    {
        new Thread(Process1).Start();
        new Thread(Process2).Start();
    }
    
    public static void Process1()
    {
        while (true)
        {
            Thread.Sleep(interval1); 
            
            ProcessStartInfo ServiceMon = new ProcessStartInfo();
            ServiceMon.FileName = @"powershell.exe";
            ServiceMon.Arguments = @"& 'C:\Scripts\Monitoring\ServiceMonitoring.ps1'";
            ServiceMon.RedirectStandardOutput = true;
            ServiceMon.RedirectStandardError = true;
            ServiceMon.UseShellExecute = false;
            ServiceMon.CreateNoWindow = true;
            Process ServiceMonProc = new Process();
            ServiceMonProc.StartInfo = ServiceMon;
            ServiceMonProc.Start();
        }
    }
    
    public static void Process2()
    {            
        while (true)
        {
            Thread.Sleep(interval2);
    
            ProcessStartInfo RAMMonitoring = new ProcessStartInfo();
            RAMMonitoring.FileName = @"powershell.exe";
            RAMMonitoring.Arguments = @"& 'C:\Scripts\Monitoring\RAMMonitoring.ps1'";
            RAMMonitoring.RedirectStandardOutput = true;
            RAMMonitoring.RedirectStandardError = true;
            RAMMonitoring.UseShellExecute = false;
            RAMMonitoring.CreateNoWindow = true;
            Process RAMMonitoringProc = new Process();
            RAMMonitoringProc.StartInfo = RAMMonitoring;
            RAMMonitoringProc.Start();
        }
    }
    

    • Marked as answer by zperryz Monday, July 1, 2013 5:15 PM
    Thursday, June 20, 2013 5:45 AM

All replies

  • Use a Timer.
    Tuesday, June 18, 2013 7:36 PM
  • I'm not sure how to use a timer...... Can you explain? Also is that the same as thread sleep? For example I need the code labeled Process 1 to run every 30 seconds, but need Process 2 to run every 30 minutes.

    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Tuesday, June 18, 2013 7:54 PM
  • My first thought would be use the Task Scheduler built into windows which is made for that very purpose, possibly writing a C# program to create the scheduled tasks.

    If that doesn't meet your needs, then you should definitely consider a timer of some sort.  As your program is currently written it will consume all the CPU power available (on a single core) of the PC.  If the PC happens to run on battery, it will wreak havoc on the battery life.

    I would probably lean towards a WinForms app that uses a notification icon.  Google/Bing C# and NotifyIcon for numerous examples of how to write such an application.  By having a (hidden) form it makes it easy to create timers as well.

    Tuesday, June 18, 2013 7:58 PM
  • This will run on a server with all the hardware I need. I just want each process to run at the different increments. I also want to avoid task scheduler in Windows. I have many other processes not listed here. What I hope to accomplish is to replace an application called Server Nanny with powershell and C#. I have the individual parts working, but I need to figure out the timing to launch the existing PS1 scripts through this C# exe I'm creating. Any help, code or links is greatly appreciated.

    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Tuesday, June 18, 2013 8:19 PM
  • Hi zperryz,

    For how to use a timer, please have a look at below articles.

    Working with a Timer in C#: The Basics

    http://www.codeguru.com/csharp/sample_chapter/article.php/c7763/Working-with-a-Timer-in-C-The-Basics.htm

    Timer in C#

    http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 19, 2013 7:21 AM
  • Hello zperryz,

    Please use threads. I included the code below. Note that 1000 millisecond = 1 second.

    Include:

    using System.Threading;
    Then here is your code (30000 milli = 30 secs, 1800000 milli = 30 mins):
    static int interval1 = 30000;
    static int interval2 = 1800000;
    
    static void Main(string[] args)
    {
        new Thread(Process1).Start();
        new Thread(Process2).Start();
    }
    
    public static void Process1()
    {
        while (true)
        {
            Thread.Sleep(interval1); 
            
            ProcessStartInfo ServiceMon = new ProcessStartInfo();
            ServiceMon.FileName = @"powershell.exe";
            ServiceMon.Arguments = @"& 'C:\Scripts\Monitoring\ServiceMonitoring.ps1'";
            ServiceMon.RedirectStandardOutput = true;
            ServiceMon.RedirectStandardError = true;
            ServiceMon.UseShellExecute = false;
            ServiceMon.CreateNoWindow = true;
            Process ServiceMonProc = new Process();
            ServiceMonProc.StartInfo = ServiceMon;
            ServiceMonProc.Start();
        }
    }
    
    public static void Process2()
    {            
        while (true)
        {
            Thread.Sleep(interval2);
    
            ProcessStartInfo RAMMonitoring = new ProcessStartInfo();
            RAMMonitoring.FileName = @"powershell.exe";
            RAMMonitoring.Arguments = @"& 'C:\Scripts\Monitoring\RAMMonitoring.ps1'";
            RAMMonitoring.RedirectStandardOutput = true;
            RAMMonitoring.RedirectStandardError = true;
            RAMMonitoring.UseShellExecute = false;
            RAMMonitoring.CreateNoWindow = true;
            Process RAMMonitoringProc = new Process();
            RAMMonitoringProc.StartInfo = RAMMonitoring;
            RAMMonitoringProc.Start();
        }
    }
    

    • Marked as answer by zperryz Monday, July 1, 2013 5:15 PM
    Thursday, June 20, 2013 5:45 AM