gpt4 book ai didi

c# - 将 C# 控制台应用程序转换为服务(Main 方法不起作用)

转载 作者:可可西里 更新时间:2023-11-01 14:43:06 26 4
gpt4 key购买 nike

我最近将我的方法“转换”或导入到默认的 Windows 服务模板中。没有语法错误并且编译正常,但是 FileSystemWatcher 方法由于某种原因不起作用,例如当正常运行时,它会将所有已创建的进程写入 process.lst,但当作为服务运行时,它不会这样做(可能与工作目录有关,因为它是服务?):

namespace WindowsService
{
class WindowsService : ServiceBase
{
/// <summary>
/// Public Constructor for WindowsService.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsService()
{
this.ServiceName = "My Service";
this.EventLog.Source = "My Service";
this.EventLog.Log = "Application";

// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;

if (!EventLog.SourceExists("My Service"))
EventLog.CreateEventSource("My Service", "Application");
}

/// <summary>
/// The Main Thread: This is where your Service is Run.
/// </summary>
static void Main()
{
ServiceBase.Run(new WindowsService());

// This checks for any existing running instances, if found the proess is terminated immidieately.
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return;

DisplayInfo();

string dirPath = "C:\\";
FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
fileWatcher.IncludeSubdirectories = true;
fileWatcher.Filter = "*.exe";
// fileWatcher.Filter = "C:\\$Recycle.Bin";
// fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
// fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
// fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
fileWatcher.EnableRaisingEvents = true;
// updated code

while (true)
{
CleanUpDel();

StartRemoveDuplicate();

CompareFiles();

bool changes = ScanFileChanges();

if (!changes)
{
TrimColon("process_trim.lst", "process_trimmed.lst");

TrimWipe();

AddTMPIgnore();

SendAlert();

CompareOrig();


}
Thread.Sleep(10000);
}
}


private static void AddTMPIgnore()
{
var myString = File.ReadAllText("process_final.lst");
File.AppendAllText("ignore_temp.lst", myString);
}



static void FileWatcher_Created(object sender, FileSystemEventArgs e)
{

using (StreamWriter fileWriter = new StreamWriter("process.lst", true))
{
var data = true;
fileWriter.Write("C:\\" + e.Name + Environment.NewLine);
}


}

最佳答案

做完最后一次服务已经有一段时间了,所以我只记得模糊但是:

有一个OnStart和一个 OnStop 方法。在其中,您必须创建一个新线程来完成这项工作。您可以使用 BackgroundWorker或创建一个 System.Threading.Thread。当我正确解释您的代码时,您将在 Main 方法中进行处理。这是不允许的。该服务将无法正确初始化。构造函数都不是执行此操作的地方。
还要确保,如果调用 OnStop,您的处理逻辑就会真正停止。否则服务控制管理器不会喜欢您的服务。

关于c# - 将 C# 控制台应用程序转换为服务(Main 方法不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6455289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com