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

时间:2017-02-19 21:53:39

标签: c# console-application windows-applications

使用定时器的典型控制台应用程序,我们可以使用' Console.ReadLine()'防止应用程序关闭。当控制台应用程序的输出类型是' Windows应用程序'?

时,是否存在等效的解决方案?

旁注:在这种情况下,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;
    }
}

enter image description here

4 个答案:

答案 0 :(得分:1)

实现您的目标:

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

答案 1 :(得分:0)

甚至更紧凑的代码:

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

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:-1)

使用更少的代码行:

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