Using FileSystemWatcher with NET Core 3.0

In .NET 3.0, FileSystemWatcher runs in a different thread. This sample shows how to update the UI from FileSystemWatcher.

.NET Core 3.0

 
.NET Core 3.0 is the new incarnation for .NET Framework with upgrades, launched on 9/23/2019. .NET Core 3.0 has compatibility with several distros of Linux and runs on macOS too. .NET Core 3.0 has support for WinForms and WPF, but you will not be able to run it on Linux and macOS.
 
I'm writing this blog because when I updated to NET Core 3.0, the app it was just quit while running and I found the reason and hence this post. 
 
If you're new and want to update, check See here how to update to NET Core 3.0
 

FileSystemWatcher in .NET Framework

 
In .NET Framework 4.x, 3.x, etc. the objects run in the same thread of the UI and events fired can change the UI directly. This can cause slow the UI. 
 

FileSystemWatcher in .NET 3.0

 
Great update of FileSystemWatcher comes in NET Core 3.0 and now it runs in a separate thread so the main UI is not affected with any delays. However, it cannot update UI directly via events. More information about FileSystemWatcher you can get on Microsoft Documentation.
 

Masterstroke

 
To implement the change is needed to use delegates, which communicate between threads. 
 
Follow in the code below how to implement it. 
  1. using System.IO;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace FileSystemWatcherSampleNETCore3._0  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.   
  12.             var fw = new FileSystemWatcher  
  13.             {  
  14.                 Filter = "*.pdf",  
  15.                 Path = "C:\\Users\\..\\Local"// Any path  
  16.                 IncludeSubdirectories = false,  
  17.                 SynchronizingObject = this,  
  18.                 EnableRaisingEvents = true  
  19.             };  
  20.   
  21.             // On .NET Core 3.0 MonikerChange runs in a different Thread  
  22.   
  23.             fw.Changed += MonikerChange;  
  24.   
  25.             // On .NET Framework 4.x, 3.x etc. It runs in the same Thread   
  26.   
  27.         }  
  28.   
  29.         
  30.         /// <summary>  
  31.         ///  The delegate is used to make the connection between Threads  
  32.         /// </summary>  
  33.         /// <param name="e"></param>  
  34.         private delegate void UpdateUIWatcherDelegate(FileSystemEventArgs e);  
  35.   
  36.         /// <summary>  
  37.         /// Event Fired in the second thread  
  38.         /// </summary>  
  39.         /// <param name="sender"></param>  
  40.         /// <param name="e"></param>  
  41.         private void MonikerChange(object sender, FileSystemEventArgs e) => UpdateUIWatcher(e);  
  42.   
  43.         /// <summary>  
  44.         /// Invoke to the new Thread  
  45.         /// </summary>  
  46.         /// <param name="e"></param>  
  47.         private void UpdateUIWatcher(FileSystemEventArgs e) =>  
  48.             _ = Invoke(new UpdateUIWatcherDelegate(MonikerChangeUIThread), e);  
  49.   
  50.         /// <summary>  
  51.         /// Now you can update  
  52.         /// Directly the UI in the Form on any other UI on the WinForms App  
  53.         /// </summary>  
  54.         /// <param name="e"></param>  
  55.         private void MonikerChangeUIThread(FileSystemEventArgs e)  
  56.         {  
  57.            // LoadPDF(e.FullPath); // Load the UI  
  58.         }  
  59.   
  60.   
  61.     }  
  62. }  
 
I've added the sample code with this post.
 
I hope this could help the community and speed up your development.
 
Happy coding!