功能
把選定文件夾下的dll
子文件夾和子文件夾中的dll填寫到窗體的treeview控件上
缺點
窗體簡單
代碼有些混亂
線程之間的調用可能沒有到最優化
實現
imagelist中的圖片需要自己重新定義
多線程的優點
可以及時響應用戶的操作
不至於產生
未響應
的尴尬
設計合理(結合需求)的話
可使軟件的性能提高
多線程的編寫
結構合理
注意同步
以上是我對這個例程和多線程的個人體會
如有不妥之處
還請大家批評指正
Imports System
IO
Public Class Form
Inherits System
Windows
Forms
Form
#Region
Windows 窗體設計器生成的代碼
Public Sub New()
MyBase
New()
該調用是 Windows 窗體設計器所必需的
InitializeComponent()
在 InitializeComponent() 調用之後添加任何初始化
End Sub
窗體重寫 dispose 以清理組件列表
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components
Dispose()
End If
End If
MyBase
Dispose(disposing)
End Sub
Windows 窗體設計器所必需的
Private components As System
ComponentModel
IContainer
注意: 以下過程是 Windows 窗體設計器所必需的
可以使用 Windows 窗體設計器修改此過程
不要使用代碼編輯器修改它
Friend WithEvents TreeView
As System
Windows
Forms
TreeView
Friend WithEvents btnOpenBrower As System
Windows
Forms
Button
Friend WithEvents ImageList
As System
Windows
Forms
ImageList
<System
Diagnostics
DebuggerStepThrough()> Private Sub InitializeComponent()
ponents = New System
ComponentModel
Container
Dim resources As System
Resources
ResourceManager = New System
Resources
ResourceManager(GetType(Form
))
Me
TreeView
= New System
Windows
Forms
TreeView
Me
ImageList
= New System
Windows
Forms
ImageList(ponents)
Me
btnOpenBrower = New System
Windows
Forms
Button
Me
SuspendLayout()
TreeView
Me
TreeView
Dock = System
Windows
Forms
DockStyle
Left
Me
TreeView
ImageList = Me
ImageList
Me
TreeView
Location = New System
Drawing
Point(
)
Me
TreeView
Name =
TreeView
Me
TreeView
Size = New System
Drawing
Size(
)
Me
TreeView
TabIndex =
ImageList
Me
ImageList
ImageSize = New System
Drawing
Size(
)
Me
ImageList
ImageStream = CType(resources
GetObject(
ImageList
ImageStream
)
System
Windows
Forms
ImageListStreamer)
Me
ImageList
TransparentColor = System
Drawing
Color
Transparent
btnOpenBrower
Me
btnOpenBrower
Location = New System
Drawing
Point(
)
Me
btnOpenBrower
Name =
btnOpenBrower
Me
btnOpenBrower
Size = New System
Drawing
Size(
)
Me
btnOpenBrower
TabIndex =
Me
btnOpenBrower
Text =
Open Brower
Form
Me
AutoScaleBaseSize = New System
Drawing
Size(
)
Me
ClientSize = New System
Drawing
Size(
)
Me
Controls
Add(Me
btnOpenBrower)
Me
Controls
Add(Me
TreeView
)
Me
Name =
Form
Me
Text =
Form
Me
ResumeLayout(False)
End Sub
#End Region
Private Sub OpenBrower()
Dim f As New FolderBrowserDialog
If f
ShowDialog = DialogResult
OK Then
Me
TreeView
Nodes
Clear()
Me
SuspendLayout()
Dim nodeTemp As TreeNode = CType(Me
Invoke(New AddTreeNodeHandler(AddressOf Me
AddFileToTreeview)
New Object() {f
SelectedPath})
TreeNode)
Me
AddFile(New DirectoryInfo(f
SelectedPath)
nodeTemp)
Me
ResumeLayout(False)
End If
End Sub
Private Sub AddFile(ByVal dirPath As DirectoryInfo
ByVal node As TreeNode)
Dim tempHandler As New AddFileHandler(AddressOf Me
AddFileToTreeNode)
For Each dir As DirectoryInfo In dirPath
GetDirectories()
Dim nodeTemp As TreeNode = CType(Me
Invoke(tempHandler
New Object() {dir
Name
CLASSIO
Directory
node})
TreeNode)
Me
AddFile(dir
nodeTemp)
Next
For Each file As FileInfo In dirPath
GetFiles(
*
dll
)
Me
Invoke(tempHandler
New Object() {file
Name
CLASSIO
File
node})
Next
End Sub
Private Function AddFileToTreeNode(ByVal str As String
ByVal cls As CLASSIO
ByVal node As TreeNode) As TreeNode
Dim nodeTemp As New TreeNode(str)
nodeTemp
ImageIndex = CInt(cls)
node
Nodes
Add(nodeTemp)
nodeTemp
SelectedImageIndex = nodeTemp
ImageIndex
Return nodeTemp
End Function
Private Function AddFileToTreeview(ByVal str As String) As TreeNode
Dim nodeTemp As New TreeNode(str)
Me
TreeView
Nodes
Add(nodeTemp)
Return nodeTemp
nodeTemp
SelectedImageIndex = CInt(CLASSIO
Directory)
End Function
Private Sub btnOpenBrower_Click(ByVal sender As System
Object
ByVal e As System
EventArgs) Handles btnOpenBrower
Click
多線程
Dim t As Threading
Thread = New Threading
Thread(AddressOf Me
OpenBrower)
t
Start()
單線程
Me
OpenBrower()
End Sub
End Class
Public Enum CLASSIO
File =
Directory =
End Enum
Public Delegate Function AddFileHandler(ByVal str As String
ByVal cls As CLASSIO
ByVal Node As TreeNode) As TreeNode
Public Delegate Function AddTreeNodeHandler(ByVal str As String) As TreeNode
From:http://tw.wingwit.com/Article/program/net/201311/13318.html