C# FileSystemWatcher not triggering correctly in Service

N0xus

I'm working on a service where in my OnStartmethod I have the following lines of code to set up my FileSystemWatcher

Log.Info($"File location {_location}");
var watcher = new FileSystemWatcher(_location);      
watcher.Changed += new FileSystemEventHandler(OnChanged);

Then in my OnChanged method I am wanting to start a timer like so:

private void OnChanged(object source, FileSystemEventArgs e)
{
    Log.Info($"A file has been placed in {_location} starting timer");
    OnTimer(null, null); //run immediately at startup
    StartEventTimer();
}

The timer code works, so I know that isn't an issue, likewise in my log I know it is checking for the correct location. What is it that I'm missing?

All I'm wanting my code to do is to trigger my timer, the moment a file is placed in my target location yet I've not been able to do so. Am I correct in that I should be using FileSystemWatcherto do this, or should I use something else as this code is within a service?

DiskJunky

There are a couple of things it could be based on what you said there.

The first thing of note is that the declaration for var watcher looks like it's not a class variable and will go out of scope when it exits OnStart(). You'll need to move the declaration outside of that.

The second item of interest is that it looks like EnableRaisingEvents isn't being set. A working example of the FileSystemWatcher is below.

public class SomeService
{
    private FileSystemWatcher _watcher;
    public void OnStart()
    {
        // set up the watcher
        _watcher = new FileSystemWatcher(_location);
        _watcher.Path = path;
        _watcher.NotifyFilter = NotifyFilters.LastWrite;
        _watcher.Filter = "*.*";
        _watcher.Changed += new FileSystemEventHandler(OnChanged);
        _watcher.EnableRaisingEvents = true;
    }
}

EDIT

As Ben Hall mentioned, it is possible that multiple events can be raised for the same file when a file is moved into the folder. As per the MSDN documentation;

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Windows Service with FileSystemWatcher starts and stops immediately after

分類Dev

c#filesystemwatcherおよびbackgroundworker

分類Dev

Azure triggered webjob not triggering when there are message in azure service bus

分類Dev

FileSystemWatcher ArgumentException

分類Dev

Service events do not affect template correctly

分類Dev

Triggering an event in c# from c++ and declaring LPCWSTR

分類Dev

C#のfileSystemWatcherでタイマーを使用する

分類Dev

FileSystemWatcherイベントがC#で起動しない

分類Dev

C#/ WPFがFileSystemWatcherを使いすぎていますか?

分類Dev

FileSystemWatcherの廃棄

分類Dev

FileSystemWatcherとrsync

分類Dev

Directory.Exists and FileSystemWatcher

分類Dev

Out-of-mem-FileSystemWatcher使用時のc#GUIの例外-マルチスレッド

分類Dev

C#FileSystemWatcherがサービスで正しくトリガーされない

分類Dev

C#:FileSystemWatcher-複数の監視フォルダーの問題

分類Dev

c#FileSystemWatcherは複数のクライアントで起動します

分類Dev

C#FileSystemWatcherプロセスはすでに使用されています

分類Dev

Why can't service host from Unity.Wcf resolve service's instance correctly?

分類Dev

Delete a member array correctly in C++

分類Dev

FileSystemWatcher losing files in its queue

分類Dev

OnChangedをリッスンすると、c#FileSystemWatcherが2回トリガーされます

分類Dev

C#:監視フォルダーが削除された後のFileSystemWatcherはどうなりますか

分類Dev

C#でFileSystemWatcherを適切に使用する方法-複数のファイルと私に「怠惰」を取得する

分類Dev

C/C++ program not correctly works on Raspberry Pi

分類Dev

onChange is not triggering in react js

分類Dev

Finding the triggering statement

分類Dev

Triggering an output task with NIDAQmx

分類Dev

`mousemove` and triggering a custom event

分類Dev

Event triggering on slide appearance

Related 関連記事

  1. 1

    Windows Service with FileSystemWatcher starts and stops immediately after

  2. 2

    c#filesystemwatcherおよびbackgroundworker

  3. 3

    Azure triggered webjob not triggering when there are message in azure service bus

  4. 4

    FileSystemWatcher ArgumentException

  5. 5

    Service events do not affect template correctly

  6. 6

    Triggering an event in c# from c++ and declaring LPCWSTR

  7. 7

    C#のfileSystemWatcherでタイマーを使用する

  8. 8

    FileSystemWatcherイベントがC#で起動しない

  9. 9

    C#/ WPFがFileSystemWatcherを使いすぎていますか?

  10. 10

    FileSystemWatcherの廃棄

  11. 11

    FileSystemWatcherとrsync

  12. 12

    Directory.Exists and FileSystemWatcher

  13. 13

    Out-of-mem-FileSystemWatcher使用時のc#GUIの例外-マルチスレッド

  14. 14

    C#FileSystemWatcherがサービスで正しくトリガーされない

  15. 15

    C#:FileSystemWatcher-複数の監視フォルダーの問題

  16. 16

    c#FileSystemWatcherは複数のクライアントで起動します

  17. 17

    C#FileSystemWatcherプロセスはすでに使用されています

  18. 18

    Why can't service host from Unity.Wcf resolve service's instance correctly?

  19. 19

    Delete a member array correctly in C++

  20. 20

    FileSystemWatcher losing files in its queue

  21. 21

    OnChangedをリッスンすると、c#FileSystemWatcherが2回トリガーされます

  22. 22

    C#:監視フォルダーが削除された後のFileSystemWatcherはどうなりますか

  23. 23

    C#でFileSystemWatcherを適切に使用する方法-複数のファイルと私に「怠惰」を取得する

  24. 24

    C/C++ program not correctly works on Raspberry Pi

  25. 25

    onChange is not triggering in react js

  26. 26

    Finding the triggering statement

  27. 27

    Triggering an output task with NIDAQmx

  28. 28

    `mousemove` and triggering a custom event

  29. 29

    Event triggering on slide appearance

ホットタグ

アーカイブ