Hi, Edwin
Based on my understanding, you want to display the results to Labels while
counting the directories and files under a given foler. If I'm off base,
please feel free to let me know.
By default, a Windows Forms Application runs on a single thread, i.e. the
UI thread, which manages all the UI objects and UI painting stuff. So when
a long calculation is performing, the UI will be blocked, and the text of
the Label won't be update until the calculation finished.
To make the UI thread free for painting the interface, we can transfer the
heavy calculation work into a separate thread. Here, we have many ways for
the threading stuff.
.NET 2.0 has introduced a BackgroundWorker component which provides a
concise multiple-threads programming model and is very easy to use. If
you're using .NET 2.0, I suggest you use the BackgroundWorker component in
your application.
In detail, call the BackgroundWorker's RunWorkerAsync method to raise the
DoWork event. In the DoWork event handler, perform the time-consuming work.
While doing the work, call the ReportProgress method to raise the
ProgressChanged event. In the ProgressChanged event handler, update the
two Labels. When the background work is done, the RunWorkerCompleted event
is fired. We also need to udpate the Labels in the RunWorkerCompleted event
handler.
The following is a sample. It requires you add two Labels and a Button on
the Form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new
DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new
ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync();
}
void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
// Display the results to the Labels.
label1.Text = string.Format("Directories: {0}", directories);
label2.Text = string.Format("Files: {0}", files);
}
void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// Display the results to the Labels.
label1.Text = string.Format("Directories: {0}", directories);
label2.Text = string.Format("Files: {0}", files);
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
string directory = "directorypath";
DirectoryInfo dir = new DirectoryInfo(directory);
if (!dir.Exists)
{
throw new DirectoryNotFoundException("The directory
does not exist.");
}
FileSystemInfo[] infos = dir.GetFileSystemInfos();
BackgroundWorker worker = sender as BackgroundWorker;
ListDirectoriesAndFiles(worker, infos);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
}
static long files = 0;
static long directories = 0;
private void ListDirectoriesAndFiles(BackgroundWorker worker,
FileSystemInfo[] FSInfo)
{
if (FSInfo == null)
{
throw new ArgumentNullException("FSInfo");
}
foreach (FileSystemInfo i in FSInfo)
{
if (i is DirectoryInfo)
{
directories++;
DirectoryInfo dInfo = (DirectoryInfo)i;
ListDirectoriesAndFiles(worker,
dInfo.GetFileSystemInfos());
}
else if (i is FileInfo)
{
files++;
}
if (directories % 10 == 0)
{
// report the work progress
// because we only want to raise the ProgressChanged
event to update the labels
// and don't care the percent of the work that has been
done in this scenario,
// we pass 0 to the ReportProgress method.
worker.ReportProgress(0);
}
}
}
}
For more information on the BackgroundWorker component, please refer to the
following MSDN document:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundwork
er.aspx
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.