Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Windows服务的While循环_C#_Windows Services - Fatal编程技术网

C# C Windows服务的While循环

C# C Windows服务的While循环,c#,windows-services,C#,Windows Services,我的windows服务有问题 protected override void OnStart(string[] args) { while (!File.Exists(@"C:\\Users\\john\\logOn\\oauth_url.txt")) { Thread.Sleep(1000); } ... 我必须等待一个特定的文件,因此while循环是必要的,但是服务将不能像这样以循环启动。我能做些什么来让服务运行,并有一个机制来检查文件是否存在?最好

我的windows服务有问题

protected override void OnStart(string[] args)
{
    while (!File.Exists(@"C:\\Users\\john\\logOn\\oauth_url.txt"))
    {
        Thread.Sleep(1000);
    }
...

我必须等待一个特定的文件,因此while循环是必要的,但是服务将不能像这样以循环启动。我能做些什么来让服务运行,并有一个机制来检查文件是否存在?

最好的选择是在服务中有一个计时器

System.Timers.Timer timer = new System.Timers.Timer();
在构造函数中,为经过的事件添加处理程序:

然后在OnStart方法中启动该计时器:

timer.Start();
在事件处理程序中,执行以下操作:

private static void TimerTicked(Object source, ElapsedEventArgs e)
{
    if (!File.Exists(@"C:\Users\john\logOn\oauth_url.txt"))
        return;

    //If the file exists do stuff, otherwise the timer will tick after another second.
}
最小的服务类看起来有点像这样:

public class FileCheckServivce : System.ServiceProcess.ServiceBase  
{
    System.Timers.Timer timer = new System.Timers.Timer(1000);

    public FileCheckServivce()
    {
        timer.Elapsed += TimerTicked;
        timer.AutoReset = true;
        timer.Enabled = true;
    }

    protected override void OnStart(string[] args)
    {
        timer.Start();
    }

    private static void TimerTicked(Object source, ElapsedEventArgs e)
    {
        if (!File.Exists(@"C:\Users\john\logOn\oauth_url.txt")) 
            return;

        //If the file exists do stuff, otherwise the timer will tick after another second.
    }
}

最好的选择是在你的服务中设置一个计时器

System.Timers.Timer timer = new System.Timers.Timer();
在构造函数中,为经过的事件添加处理程序:

然后在OnStart方法中启动该计时器:

timer.Start();
在事件处理程序中,执行以下操作:

private static void TimerTicked(Object source, ElapsedEventArgs e)
{
    if (!File.Exists(@"C:\Users\john\logOn\oauth_url.txt"))
        return;

    //If the file exists do stuff, otherwise the timer will tick after another second.
}
最小的服务类看起来有点像这样:

public class FileCheckServivce : System.ServiceProcess.ServiceBase  
{
    System.Timers.Timer timer = new System.Timers.Timer(1000);

    public FileCheckServivce()
    {
        timer.Elapsed += TimerTicked;
        timer.AutoReset = true;
        timer.Enabled = true;
    }

    protected override void OnStart(string[] args)
    {
        timer.Start();
    }

    private static void TimerTicked(Object source, ElapsedEventArgs e)
    {
        if (!File.Exists(@"C:\Users\john\logOn\oauth_url.txt")) 
            return;

        //If the file exists do stuff, otherwise the timer will tick after another second.
    }
}

我会考虑使用FraseStaseWestCurver,这正是它想要监视的文件系统上的更改。在文件夹上引发事件后,您可以检查该特定文件是否存在


在MSDN中的缺省示例实际上显示了.txt文件

< p>的监视。我将考虑使用FraseSistMeW跟踪器,这正是它想要监视的文件系统上的更改。在文件夹上引发事件后,您可以检查该特定文件是否存在


MSDN中的默认示例实际上显示了对.txt文件的监视

不要在OnStart中执行此操作。不要使用\\,分隔符是\不要在OnStart中执行此操作。不要使用\\,分隔符是\