简体   繁体   中英

Code works in a console application, but does not work when running in windows service

I wrote a windows service. Part of the code works : (example from msdn)

protected override void OnStart(string[] args)
    {
        file = new StreamWriter(new FileStream("MyFirstService.log",
                          System.IO.FileMode.Append));
        this.file.WriteLine("MyFirstService стартовал");
        this.file.Flush(); 
        Thread thread = new Thread(new ThreadStart(WatchProcess));
        thread.Start();
    }

I am adding this code :

private void WatchProcess()
    {
            var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true);
            key.SetValue("dWatch", System.Reflection.Assembly.GetEntryAssembly().Location);
            System.IO.FileSystemWatcher fWatcher;
            fWatcher = new System.IO.FileSystemWatcher();
            fWatcher.IncludeSubdirectories = true;
            fWatcher.Path = @"C:\Windows";
            fWatcher.Filter = "*.*";
            fWatcher.NotifyFilter = System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName;
            fWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fWatcher.Created += new FileSystemEventHandler(OnChanged);
            fWatcher.Deleted += new FileSystemEventHandler(OnChanged);
            fWatcher.Renamed += new RenamedEventHandler(OnRenamed);
            fWatcher.EnableRaisingEvents = true;
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        WatcherChangeTypes wtc = e.ChangeType;
        string str = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
        StreamWriter sw = new StreamWriter(str);
        Console.WriteLine("File: " + e.FullPath + " " + wtc.ToString());
    }


    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        string str = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
        StreamWriter sw = new StreamWriter(str);
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

The code above works when I run it in console application, but in windows service it does not work. And service automatically stops after 30 seconds.

 protected override void OnStart(string[] args)
    {
        file = new StreamWriter("C:\\Public\\fswThread." + DateTime.Now.Millisecond.ToString() + ".txt");
        System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
        watcher.Path = @"C:\Windows";
        watcher.Filter = "*.*";
        watcher.IncludeSubdirectories = true;
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                             | NotifyFilters.FileName   | NotifyFilters.DirectoryName;
        watcher.Changed += new FileSystemEventHandler( OnChanged );
        watcher.EnableRaisingEvents = true;
    }

    protected override void OnStop()
    {
        file.Close();
    }

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
        file.WriteLine( "C: " + e.FullPath );
        file.AutoFlush = true;

        if (file.BaseStream.Length > 100)
        {
            file.Flush();
            file.Close();
            file = new StreamWriter("C:\\Public\\fswThread." + DateTime.Now.Millisecond.ToString() + ".txt");
        }


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM