1

With a typical console application using timers, we can use 'Console.ReadLine()' to prevent the application from closing. Is there an equivalent solution when the console application's output type is 'Windows Application'?

Side-note: A windows service isn't the solution in this case, as I'm launching processes.

class Program
{        
    private static Timer _Timer;       

    static void Main(string[] args)
    {   
        SetupTimer();                        
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {           
        Process.Start(@"C:\WINDOWS\system32\notepad.exe");
    }

    private static void SetupTimer()
    {
        _Timer = new Timer();
        _Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        _Timer.Interval = 3000;
        _Timer.Enabled = true;
    }
}

enter image description here

ManxJason
  • 928
  • 12
  • 33
  • 1
    A windows application has a window so as long as the window is not closed, the timer will run. Closing the window is a user command to close the window the same as entering a key into a console is a command to close the console. Not sure what you are asking here. – CodingYoshi Feb 19 '17 at 21:56
  • @CodingYoshi See here. My application has no window to close. http://stackoverflow.com/questions/2686289/how-to-run-a-net-console-app-in-the-background – ManxJason Feb 19 '17 at 21:58
  • 2
    It is not very clear why you want to use a Timer. You have no UI to keep happy so Thread.Sleep() can do it too. – Hans Passant Feb 19 '17 at 22:18
  • @HansPassant Why I want to use a timer is irrelevant to my question, but it's because I want to monitor another process and once it terminates, launch another application. Thread.Sleep() will do the job. Thanks – ManxJason Feb 19 '17 at 23:12
  • 1
    Store the `Process` instance returned by `Process.Start()` and then use its [WaitForExit()](https://msdn.microsoft.com/en-us/library/fb4aw7b8(v=vs.110).aspx) method?.... – Idle_Mind Feb 19 '17 at 23:30

4 Answers4

1

To achieve what you want:

using System;
using System.Diagnostics;
using System.Timers;

public class Program
{
    private static Timer _Timer;
    private static bool Launched = false;
    static void Main(string[] args)
    {
        SetupTimer();
        WaitUntilItIsLaunched:
        if (!Launched)
        {
            System.Threading.Thread.Sleep(100);
            goto WaitUntilItIsLaunched;
        }
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        Launched = true;
    }

    private static void SetupTimer()
    {
        _Timer = new Timer();
        _Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        _Timer.Interval = 3000;
        _Timer.Enabled = true;
    }
}
user7358693
  • 503
  • 2
  • 3
  • Ok so we simply continually sleep the thread until my condition is met. Not very elegant, but thanks. – ManxJason Feb 19 '17 at 22:02
  • You could use Tasks and Thread controlling, but this would be more difficult to you to understand. But yet this is not a bad solution. – user7358693 Feb 19 '17 at 22:11
0

Or even more compact code:

using System;
using System.Diagnostics;
using System.Threading;
public class Program
{
    static void Main(string[] args)
    {
        DateTime LaunchAt = DateTime.Now.AddSeconds(3);
        WaitLaunch:
        Thread.Sleep(100);
        if (DateTime.Now >= LaunchAt)
            Process.Start(@"C:\WINDOWS\system32\notepad.exe");
        else
            goto WaitLaunch;
    }
}
user7358693
  • 503
  • 2
  • 3
  • The Thread.Sleep(100); is important piece of code because without it, the processor keeps on making the loop millions of time per second causing the processor to be used 100% of its capacity. – user7358693 Feb 19 '17 at 22:28
0

If you want to launch several programs in different times and wait all them to launch:

using System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class Program
{
    static Dictionary<string,DateTime> ExecutablesToLaunch = new Dictionary<string,DateTime>();
    static List<string> ExecutablesLaunched = new List<string>();
    static void Main(string[] args)
    {
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\notepad.exe", DateTime.Now.AddSeconds(3));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\control.exe", DateTime.Now.AddSeconds(5));
        ExecutablesToLaunch.Add(@"C:\WINDOWS\system32\calc.exe", DateTime.Now.AddSeconds(10));

        WaitAllToLaunch:
        if (ExecutablesToLaunch.Count == ExecutablesLaunched.Count)
            return;
        Thread.Sleep(100);
        foreach (var Executable in ExecutablesToLaunch)
        {
            if (ExecutablesLaunched.Contains(Executable.Key))
                continue;
            if (DateTime.Now >= Executable.Value)
            {
                Process.Start(Executable.Key);
                ExecutablesLaunched.Add(Executable.Key);
            }
            else
                goto WaitAllToLaunch;
        }
    }

}
user7358693
  • 503
  • 2
  • 3
-1

With less lines of code:

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Timers;
public class Program
{
    static void Main(string[] args)
    {
        int MsToWait = 3000;
        int MsElapsed = 0;
        WaitLaunch:
        System.Threading.Thread.Sleep(100);
        MsElapsed += 100;
        if (MsElapsed >= MsToWait)
        {
            Process.Start(@"C:\WINDOWS\system32\notepad.exe");
            return;
        }
        else
            goto WaitLaunch;
    }
}
user7358693
  • 503
  • 2
  • 3
  • Don't use `goto` when you can use you could use regular loop and have more readable code. – Phil1970 Feb 19 '17 at 23:36
  • Also don't post multiple answers to the same question. – Phil1970 Feb 19 '17 at 23:36
  • I will post as many answers I want as long as they are correct and they differ significantly. If you think goto is unreadeable then you must put a complete answer, you are not exactly helping anyone here if you don't. A goto in this case is much more readeable than a timer. – user7358693 Feb 20 '17 at 00:00