Reputation: 29
I am using FileSystemWatcher to monitor some folders and in case of activity on that folders, another FileSystemWatcher_Created is called to copy the folder with all files inside. These folders are large, so I am having problems in the form (freezing).
I am using below code to monitor folders:
private static void MonitorDirectory1(string path1)
{
try
{
FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
fileSystemWatcher1.Path = path1;
fileSystemWatcher1.Created += FileSystemWatcher_Created1;
fileSystemWatcher1.EnableRaisingEvents = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
And below is the code to copy folders and all the contets to a new path:
private static void FileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
var pathSource = e.FullPath;
var pathDestination = @"C:\Log";
foreach (var path1 in Directory.GetDirectories(pathSource, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(path.Replace(pathSource, pathDestination));
foreach (var files in Directory.GetFiles(pathSource, "*.*", SearchOption.AllDirectories))
File.Copy(files, files.Replace(pathSource, pathDestination), true);
}
My question is: how can I use threads to monitor and trigger another function inside static reference?
Upvotes: 0
Views: 45
Reputation: 81573
You could use the async and await pattern and Task.Run
to offload the work to the threadpool.
Note 1 : Usually you wouldn't use async void
, or use a task for non IO bound work, however this is an exception.
Firstly, async void
is suited for events, so.. tick
Secondly, Even though the implementation of method has no explicit async
and it will tie up a thread, we can use the async and await pattern to run the task (most likely in a thread pool thread), using the current syncronization context (if applicable) and you need a continuation back on the main thread, and freeing up the ui... tick
Note 2 : Because we are using the async void
, if you get 2 events for the same file, you may or likely run into locking and general thread safety issues, you need to be defensive against this
private static async void FileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
await Task.Run(() =>
{
WatcherChangeTypes wct = e.ChangeType;
Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
var pathSource = e.FullPath;
var pathDestination = @"C:\Log";
foreach (var path1 in Directory.GetDirectories(pathSource, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(path.Replace(pathSource, pathDestination));
foreach (var files in Directory.GetFiles(pathSource, "*.*", SearchOption.AllDirectories))
File.Copy(files, files.Replace(pathSource, pathDestination), true);
});
}
Note 3 : This is not intended to be a complete solution, and only a suggestion for using the above technologies, you should research Tasks, and the async await pattern for your own benefit to see how it can help you
Upvotes: 1