C#Windows服务无法正常运行 [英] C# Windows Service not running correctly

查看:403
本文介绍了C#Windows服务无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了Windows服务(使用Visual Studio 2010 Premium,C#、. NET 4)来监视文件夹.它所做的只是检测文件夹中的更改时间(添加文件/文件夹,删除某些内容等)并将检测结果写入文本文件.不是事件日志,只是普通的.txt文件.

I've written a Windows Service (using Visual Studio 2010 Premium, C#, .NET 4) to monitor a folder. All it does is detect when a change is made in the folder (a file/folder added, something deleted, etc) and writes the detections to a text file. Not an Event Log, just a normal .txt file.

现在,它可以很好地安装(我认为是),并显示在计算机管理"中并允许我运行它.它应该将监视开始"写入文件-但事实并非如此.然后,当我尝试停止该服务时,它给了我这个错误:

Now, it installs fine (I presume), and it shows up in Computer Management and allows me to run it. It supposed to write "Monitoring started" to the file - but it doesn't. Then, when I try to stop the service it gives me this error:

"Windows无法在本地计算机上停止FileMonitoring服务.该服务未返回错误.这可能是Windows内部错误或内部服务错误.如果问题仍然存在,请与系统管理员联系."

并没有停止.然后,当我尝试再次停止它时,出现此错误:

And doesn't stop. Then, when I attempt to stop it again, I get this error:

"Windows无法在本地计算机上停止FileMonitoring服务.错误1061:该服务目前无法接受控制消息."

然后停止.

有人知道如何解决此问题吗?在使用该服务之前,我只是做了一个简单的服务,该服务在服务启动时写入.txt文件启动",在服务停止时写入.txt文件,并且运行良好.所有添加到其中的都是FileSystemWatcher的使用.

Does anyone have any idea how to resolve this issue? Before this service, I just made a simple service that wrote to a .txt file "Started" when the service started and "Stopped" when the service stopped, and it worked perfectly. All that's added into this is the used of FileSystemWatcher.

任何帮助将不胜感激,只要您问我,我就会提供您所需的任何信息/代码段.

Any help would be much appreciated, and I'll give any info/code snippets you need, just ask.

提前谢谢!

以下是一些适合您的代码:

Here's some code for you:

protected override void OnStart(string[] args)
{
    watcher = new FileSystemWatcher();
    watcher.Path = @"C:\temp\services\Watched";

    watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Created += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Deleted += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Renamed += new RenamedEventHandler(LogFileSystemRenaming);
    watcher.Error += new ErrorEventHandler(LogBufferError);
    watcher.EnableRaisingEvents = true;

    LogEntry("Monitoring Started.");
}

protected override void OnStop()
{
    watcher.EnableRaisingEvents = false;
    watcher.Dispose();

    LogEntry("Monitoring Stopped.");
}

void LogEntry(string message)
{
    counter++;

    FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs);

    sw.WriteLine(counter + ". " + message);

    fs.Close();
    sw.Close();
}

好的,有人指出了我没有关闭自己的Streams的事实.傻了,傻了我.现在,我在那之后运行了该服务,但没有得到相同的错误,我得到了这个错误:

Okay, someone noted the fact I had not closed my Streams. Silly, silly me. Now, I ran the service after that, and didn't get the same error, I got this one:

本地计算机上的FileMonitoring服务已启动,然后停止.某些服务由于未被其他服务或程序使用而自动停止."

这似乎简单得多,但我仍然茫然.有什么想法吗?

This seems a lot simpler but I'm still at a loss. Any ideas?

非常感谢!

推荐答案

您必须考虑的一件事(尽管我不认为这对您而言是个问题)是对文件的并发访问.

One thing you will have to consider (although I don't think it's a problem in your case) is concurrent access to the file.

为确保流被关闭,优良作法是也将它们包装在using语句中.

To ensure that streams are closed it is good practice to wrap them in using statements as well.

protected override void OnStart(string[] args)
{
    watcher = new FileSystemWatcher();
    watcher.Path = @"C:\temp\services\Watched";

    watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Created += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Deleted += new FileSystemEventHandler(LogFileSystemChanges);
    watcher.Renamed += new RenamedEventHandler(LogFileSystemRenaming);
    watcher.Error += new ErrorEventHandler(LogBufferError);
    watcher.EnableRaisingEvents = true;

    LogEntry("Monitoring Started.");
}

protected override void OnStop()
{
    watcher.EnableRaisingEvents = false;
    watcher.Dispose();

    LogEntry("Monitoring Stopped.");
}

private object lockObject = new Object();

void LogEntry(string message)
{
    lock (lockObject)
    {
        counter++;

        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine(counter + ". " + message);
        }
    }
}

您可以使用的另一种技巧是System.Diagnostics.Debugger.Break();.方法.如果将其放在服务的开头,它会提示您将Visual Studio附加到它上,然后可以逐步完成它.

Another trick you can use is the System.Diagnostics.Debugger.Break(); method. If you put this in the beginning of your service it should prompt you to attach Visual Studio to it and you can step through it.

希望有帮助.

这篇关于C#Windows服务无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆