⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmexample2.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Threading;

namespace CodeForChapter11cs
{
  public partial class frmExample2 : Form
  {
    public frmExample2()
    {
      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;
    }

    // re-write this menu click event handler method
    private void menuItem1_Click(object sender, EventArgs e)
    {
      // load list
      label1.Text = "";
      listBox1.DataSource = null;
      stopRequested = false;

      Thread t = new Thread(this.GetAllFiles); //delegate inference
      t.Name = "Worker: allFiles populator";
      t.Start();
    }

    //... extracted form menu event hnadler and is now Control Invoked
    private void UpdateBox(object sender, EventArgs e)
    {
      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");

      this.Invoke(new EventHandler(this.UpdateBox)); //at the end, update the UI
    }

    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());

      // replaces previously commented out line
      this.Invoke(
        new EventHandler<LabelEventArgs>(UpdateLabel),
        new object[]{label1, new LabelEventArgs(allFiles.Count)});


      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;
        }
      }
    }

    #region Supports Control.Invoke
    // Could have used delegate with int parameter but best follow the .NET pattern of having an EventArgs class
    private class LabelEventArgs : EventArgs
    {
      public LabelEventArgs(int items)
      {
        NumberOfItems = items;
      }
      public int NumberOfItems;
    }

    private void UpdateLabel(object sender, LabelEventArgs e)
    {
      label1.Text = e.NumberOfItems.ToString();
    } 
    #endregion
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -