I am writing a program to find all duplicate files in specific folder.
My idea is to collect all file names into string[]
. Then run multiple threads which hash each string[] element file (hash by content) and by checking if hash as a key is already in dictionary find duplicates.
I initialize my search with start button:
//Start button:
private void btnStart_Click(object sender, EventArgs e)
{
Search.initialize(this.labelPath.Text, this.textResultsFile.Text);
}
//My run method
public void run(string basePath, string resultFile)
{
//string[] contains all file paths
string[] filePaths = Directory.GetFiles(@basePath, "*", SearchOption.AllDirectories);
HashingCompletedCallback callback = new HashingCompletedCallback(CheckDuplicate);
Multi multi = new Multi(string[0], callback);
Thread T1 = new Thread(new ThreadStart(multi.HashFile));
T1.Start();
}
So in this case I'm taking string[0]
, which is 1st path in paths list (example: C:/BasePath/first.txt'
), hashing file and in callback
checking if hash exists if dictionary, if yes, duplicate is found, else I just add hash to library.
So here are some problems I am facing right now:
How do I implement stop/continue and restart buttons in such search? I have already created these buttons using Windows.Forms, but I'm not sure how to control them and not fail with user interface locking. For example if I start
foreach
string[] as element
-> my user interface freezes and no actions can be done.Currently I have written my program so I can use one worker thread. How do I automatically create more threads and involve them in search? In this case I guess I have to put lock into my callback, so my dictionary is checked and written only by one thread at a time. In this can will I just lock my method function or entire class will be locked?
BTW: I did read about: how to pause/resume a thread but not sure how to use it.