📄 frmexample1.cs
字号:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Threading;
namespace CodeForChapter11cs
{
public partial class frmExample1 : Form
{
public frmExample1()
{
InitializeComponent();
}
private ArrayList allFiles = new ArrayList(); // holds the results
private bool stopRequested = false; // user hit the cancel button
private void menuItem2_Click(object sender, EventArgs e)
{
// cancel
stopRequested = true;
}
private void menuItem1_Click(object sender, EventArgs e)
{
// load list
label1.Text = "";
listBox1.DataSource = null;
stopRequested = false;
#region Thread solution
Thread t = new Thread(this.GetAllFiles); //delegate inference
t.Name = "Worker: allFiles populator";
t.Start();
t.Join(); // whatever thread is running, blocks waiting for t to exit
#endregion
label1.Text = allFiles.Count.ToString();
listBox1.DataSource = allFiles;
Debug.WriteLine("Got results " + listBox1.Items.Count.ToString());
}
private void GetAllFiles()
{
allFiles.Clear();
this.PopulateAllFilesFor(@"\");
Debug.WriteLine("Finished");
}
private void PopulateAllFilesFor(string path)
{
if (stopRequested)
{
Debug.WriteLine("cancelled 1");
return;
}
Debug.WriteLine("Processing new path");
allFiles.Add(path);
string[] files;
files = Directory.GetFiles(path);
allFiles.AddRange(files);
Debug.WriteLine(allFiles.Count.ToString());
//label1.Text = allFiles.Count.ToString(); // comment out to avoid the NotSupportedException. Will revisit.
if (stopRequested)
{
Debug.WriteLine("cancelled 2");
return;
}
foreach (string subDirectory in Directory.GetDirectories(path))
{
this.PopulateAllFilesFor(subDirectory); //recursion
if (stopRequested)
{
Debug.WriteLine("cancelled 3");
return;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -