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

📄 frmexample1.vb

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

Namespace CodeForChapter11cs
  Public Partial Class frmExample1
	  Inherits Form
	Public Sub New()
	  InitializeComponent()
	End Sub

	Private allFiles As ArrayList = New ArrayList() ' holds the results
	Private stopRequested As Boolean = False ' user hit the cancel button

	Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem2.Click
	  ' cancel
	  stopRequested = True
	End Sub

	Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
	  ' load list
	  label1.Text = ""
	  listBox1.DataSource = Nothing
	  stopRequested = False

      '	 "Thread solution"
	  Dim t As Thread = New Thread(AddressOf Me.GetAllFiles) 'delegate inference
	  t.Name = "Worker: allFiles populator"
	  t.Start()

	  t.Join() ' whatever thread is running, blocks waiting for t to exit

	  label1.Text = allFiles.Count.ToString()
	  listBox1.DataSource = allFiles
	  Debug.WriteLine("Got results " & listBox1.Items.Count.ToString())
	End Sub

	Private Sub GetAllFiles()
	  allFiles.Clear()
	  Me.PopulateAllFilesFor("\")
	  Debug.WriteLine("Finished")
	End Sub

	Private Sub PopulateAllFilesFor(ByVal path As String)
	  If stopRequested Then
		Debug.WriteLine("cancelled 1")
		Return
	  End If
	  Debug.WriteLine("Processing new path")

	  allFiles.Add(path)

	  Dim files As String()
	  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 Then
		Debug.WriteLine("cancelled 2")
		Return
	  End If

	  For Each subDirectory As String In Directory.GetDirectories(path)
		Me.PopulateAllFilesFor(subDirectory) 'recursion
		If stopRequested Then
		  Debug.WriteLine("cancelled 3")
		  Return
		End If
	  Next subDirectory
	End Sub
  End Class
End Namespace

⌨️ 快捷键说明

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