将 System.Timers 与控制台应用程序和输出类型一起使用 'Windows Application'

Using System.Timers with a console application and output type 'Windows Application'

对于使用计时器的典型控制台应用程序,我们可以使用 'Console.ReadLine()' 来防止应用程序关闭。当控制台应用程序的输出类型为'Windows Application'时是否有等效的解决方案?

旁注:在这种情况下,windows 服务不是解决方案,因为我正在启动流程。

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

实现你想要的:

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

代码行更少:

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

或者更紧凑的代码:

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

如果您想在不同时间启动多个程序并等待所有程序启动:

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

}