The service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs

假如想象 提交于 2019-12-12 01:44:22

问题


[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
    private ServiceProcessInstaller serviceProcessInstaller;
    private ServiceInstaller serviceInstaller;

    public ProjectInstaller()
    {
        serviceProcessInstaller = new ServiceProcessInstaller();
        serviceInstaller = new ServiceInstaller();
        // Here we can set properties on serviceProcessInstaller
        //or register event handlers
        serviceProcessInstaller.Account = ServiceAccount.LocalService;

        serviceInstaller.ServiceName = MyNewService.MyServiceName;
        this.Installers.AddRange(new Installer[] {
            serviceProcessInstaller, serviceInstaller });
    }

    private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {

    }
}



public partial class MyNewService : ServiceBase
{

    FileSystemWatcher myWatcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo");
    public const string MyServiceName = "MyNewService";
    private FileSystemWatcher watcher = null;


    public MyNewService()
    {
        InitializeComponent();
        myWatcher.NotifyFilter = NotifyFilters.LastAccess
           | NotifyFilters.LastWrite
           | NotifyFilters.FileName
           | NotifyFilters.DirectoryName;
    }
    //private static void OnChanged(object source, FileSystemEventArgs e)
    //{

    //    WatcherChangeTypes wct = e.ChangeType;
    //    Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
    //}
    protected void FileCreated(object sender, FileSystemEventArgs e)
    {
        if (e.ChangeType == WatcherChangeTypes.Created)
        {
            if (Directory.Exists(e.FullPath))
            { Console.WriteLine("Directory Exists"); }            // a directory
            else { Console.WriteLine("File"); }
            // a file
        }
    }


    protected override void OnStart(string[] args)
    {

        this.ServiceName = MyServiceName;


        FileSystemWatcher watcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo", "*.txt");

        //Watch for changes in LastAccess and LastWrite times, and
        //the renaming of files or directories.
        watcher.NotifyFilter = NotifyFilters.LastAccess
                             | NotifyFilters.LastWrite
                             | NotifyFilters.FileName
                             | NotifyFilters.DirectoryName;

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }

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

        LogEvent("Monitoring Stopped");
    }

    void OnChanged(object sender, FileSystemEventArgs e)
    {
        string mgs = string.Format("File {0} | {1}",e.FullPath, e.ChangeType);
        LogEvent(mgs);
    }

    void OnRenamed(object sender, RenamedEventArgs e)
    {
        string log = string.Format("{0} | Renamed from {1}",
                                   e.FullPath, e.OldName);
        LogEvent(log);
    }
    private void LogEvent(string message)
    {
        string eventSource = "File Monitor Service";
        DateTime dt = new DateTime();
        dt = System.DateTime.UtcNow;
        message = dt.ToLocalTime() + ": " + message;

        EventLog.WriteEntry(eventSource, message);
    }

    private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e)
    {

    }
}


static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new MyNewService() 
        };
        ServiceBase.Run(new ServiceBase[] { new MyNewService() });
    }
}

回答1:


The problem here is that this.ServiceName = MyServiceName; should be in the Constructor, not the OnStart method. The constructor will set it for each instance, but it throws an exception because the service is already considered running by the time OnStart is called.

The ServiceName identifies the service to the Service Control Manager. The value of this property must be identical to the name recorded for the service in the ServiceInstaller.ServiceName property of the corresponding installer class. In code, the ServiceName of the service is usually set in the main() function of the executable.

--MSDN Reference



来源:https://stackoverflow.com/questions/23498060/the-service-on-local-computer-started-and-then-stopped-some-services-stop-autom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!