I need to know when is this thread finished with whatever is it doing. also have an active timer on background.
note: “scanner” is an recursive function
// button click
//timer
timer.Enabled = true;
//thread
System.Threading.ThreadStart start = delegate { scanner(@"c:\", "*.txt;*.html;"); };
System.Threading.Thread thread = new System.Threading.Thread(start);
thread.Start();
-------------
private static long Counter = 0;
private static string scanstatus = string.Empty;
private static void scanner(string folder, string patterns)
{
// Get the patterns.
string[] pattern_array = patterns.Split(';');
foreach (string pattern in pattern_array)
{
try
{
foreach (string fileName in System.IO.Directory.GetFiles(folder, pattern))
{
//trim path
scanstatus = (fileName.Length > 50) ? "../" + fileName.Substring(fileName.Length - 49, 49) : fileName;
// delay
//System.Threading.Thread.Sleep(5);
Counter++;
}
}
catch (System.Exception E)
{
Console.WriteLine(E.Message);
}
}
try
{
foreach (string directory in System.IO.Directory.GetDirectories(folder))
scanner(directory, patterns);
}
catch (System.Exception E)
{
Console.WriteLine(E.Message);
}
}
Take a look at the MSDN Article on Thread Synchronization:
Thread Synchronization (C#)
Pay close attention to the “Synchronization Events and Wait Handles” section