Disposing of FileSystemWatcher

后端 未结 2 531
我在风中等你
我在风中等你 2020-12-17 02:21

So my understanding is that whenever using a class that implements IDisposable, it\'s parent also needs to implement IDisposable interface. (FileWatcher using FileSystemWatc

2条回答
  •  一个人的身影
    2020-12-17 03:19

    Yes, implementing IDisposable is the right solution in this case (in my opinion). Your object is long-lived and has to live outside the scope of any particular function call so all function-scope level solutions (using, try..finally, etc.) are out.

    For this, IDisposable is a standard pattern in .NET and you can easily dispose of the nested object when FileWatcher is disposed of.

提交回复
热议问题