Reputation: 812
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.
Upvotes: 0
Views: 326
Reputation: 150108
How do I implement stop/continue and restart buttons in such search?
You can use a ManualResetEvent to have your UI thread control whether the worker threads are running or paused. The worker threads would each call WaitOne before processing the next file.
A ManualResetEvent functions like an ordinary gate. Calling Set opens the gate, allowing any number of threads calling WaitOne to be let through. Calling Reset closes the gate. Threads that call WaitOne on a closed gate will block; when the gate is next opened, they will be released all at once.
(Source)
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.
Consider a ConcurrentDictionary rather than a Dictionary, as it is designed to be thread safe.
These days I would use a Task rather than a Thread. Assuming you are sticking with a Thread, you can do something like.
Thread T1 = new Thread(new ThreadStart(multi.HashFile));
T1.Start();
// Repeat for T2, T3, etc as desired.
T1.Join();
// Repeat for T2, T3, etc.
Upvotes: 1