I need you to debug my idea about this project.
I’ve written a backup manager project which I give a folder and it copies every file and folder of it to another location and so on.
It works (does the copy job well) but during copying which takes about 1 minute the application UI does not respond. I’ve heard about threads and I’ve seen the word parallel programming (just the word and no more), now I want some explanation, comparison and examples to become able to switch my code.
I have done very simple actions with threads before but it was a long time ago and I am not confident enough on threading. Here is my code :
private void CopyFiles(string path, string dest)
{
System.IO.Directory.CreateDirectory(dest + "\\" + path.Split('\\')[path.Split('\\').Count()-1]);
dest = dest + "\\" + path.Split('\\')[path.Split('\\').Count() - 1];
foreach (string file in System.IO.Directory.GetFiles(path))
{
System.IO.File.Copy(file, dest + "\\" + file.Split('\\')[file.Split('\\').Count() - 1]);
}
foreach (string folder in System.IO.Directory.GetDirectories(path))
{
CopyFiles(folder, dest);
}
}
I run this in a timer based on a special interval, if I come up using threading, should I omit timer? Lead me, I’m confused.
Since you are not confident with threading enough, I highly recommend you read Joe Albahari’s Threading in C# Tutorial. Parallel programming is when you do multiple operations in ‘parallel’ or at the same time (mostly for spreading large amounts of calculations over several CPU or GPU cores). In this case you want threading to make your UI responsive while copying all the files. Essentially, you would have something set out like this: (After you read the threading in C# tutorial)
The UI runs on its own thread. All of the code that is put into your application will run on the UI thread (unless you are explicitly using threading). Since your
CopyFilesmethod takes a long time, it will stop the UI until the copying job is completed. Using threading will run theCopyFileson a separate thread to the UI thread, therefore making the UI thread responsive.Edit: As for your timer, how often does it run?