Hi Edwin,
Thank you for posting here!
I notice that you post the same issue in the
microsoft.publi c.dotnet.framew ork.windowforms newsgroup, which I have
replied to. For your convenience, I include the reply here.
=============== =============== ===========
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 BackgroundWorke r 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 BackgroundWorke r component in
your application.
In detail, call the BackgroundWorke r'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 RunWorkerComple ted event
is fired. We also need to udpate the Labels in the RunWorkerComple ted 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()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
BackgroundWorke r backgroundWorke r1 = new BackgroundWorke r();
backgroundWorke r1.WorkerReport sProgress = true;
backgroundWorke r1.DoWork += new
DoWorkEventHand ler(backgroundW orker1_DoWork);
backgroundWorke r1.ProgressChan ged += new
ProgressChanged EventHandler(ba ckgroundWorker1 _ProgressChange d);
backgroundWorke r1.RunWorkerCom pleted += new
RunWorkerComple tedEventHandler (backgroundWork er1_RunWorkerCo mpleted);
backgroundWorke r1.RunWorkerAsy nc();
}
void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
// Display the results to the Labels.
label1.Text = string.Format(" Directories: {0}", directories);
label2.Text = string.Format(" Files: {0}", files);
}
void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
// Display the results to the Labels.
label1.Text = string.Format(" Directories: {0}", directories);
label2.Text = string.Format(" Files: {0}", files);
}
void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs e)
{
try
{
string directory = "directorypath" ;
DirectoryInfo dir = new DirectoryInfo(d irectory);
if (!dir.Exists)
{
throw new DirectoryNotFou ndException("Th e directory
does not exist.");
}
FileSystemInfo[] infos = dir.GetFileSyst emInfos();
BackgroundWorke r worker = sender as BackgroundWorke r;
ListDirectories AndFiles(worker , infos);
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
finally
{
}
}
static long files = 0;
static long directories = 0;
private void ListDirectories AndFiles(Backgr oundWorker worker,
FileSystemInfo[] FSInfo)
{
if (FSInfo == null)
{
throw new ArgumentNullExc eption("FSInfo" );
}
foreach (FileSystemInfo i in FSInfo)
{
if (i is DirectoryInfo)
{
directories++;
DirectoryInfo dInfo = (DirectoryInfo) i;
ListDirectories AndFiles(worker ,
dInfo.GetFileSy stemInfos());
}
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.ReportPr ogress(0);
}
}
}
}
For more information on the BackgroundWorke r component, please refer to the
following MSDN document:
http://msdn.microsoft.com/en-us/libr...backgroundwork
er.aspx
=============== =============== =============== ==========
If you have any question, please reply to that thread and I will follow up
with you in time.
Thank you for using our MSDN Managed Newsgroup Support Service!
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:
ms****@microsof t.com.
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.