熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET中采用多線程填寫treeview控件

2022-06-13   來源: .NET編程 
功能把選定文件夾下的dll子文件夾和子文件夾中的dll填寫到窗體的treeview控件上

  缺點窗體簡單代碼有些混亂線程之間的調用可能沒有到最優化

  實現imagelist中的圖片需要自己重新定義

  多線程的優點可以及時響應用戶的操作不至於產生未響應的尴尬設計合理(結合需求)的話可使軟件的性能提高

  多線程的編寫結構合理注意同步

  以上是我對這個例程和多線程的個人體會如有不妥之處還請大家批評指正

Imports SystemIO
Public Class Form
Inherits SystemWindowsFormsForm

#Region Windows 窗體設計器生成的代碼

Public Sub New()
 MyBaseNew()

 該調用是 Windows 窗體設計器所必需的
 InitializeComponent()

 在 InitializeComponent() 調用之後添加任何初始化

End Sub

窗體重寫 dispose 以清理組件列表
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 If disposing Then
  If Not (components Is Nothing) Then
   componentsDispose()
  End If
 End If
 MyBaseDispose(disposing)
End Sub

Windows 窗體設計器所必需的
Private components As SystemComponentModelIContainer

 注意: 以下過程是 Windows 窗體設計器所必需的
 可以使用 Windows 窗體設計器修改此過程
 不要使用代碼編輯器修改它
 Friend WithEvents TreeView As SystemWindowsFormsTreeView
 Friend WithEvents btnOpenBrower As SystemWindowsFormsButton
 Friend WithEvents ImageList As SystemWindowsFormsImageList
 <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
 ponents = New SystemComponentModelContainer
 Dim resources As SystemResourcesResourceManager = New SystemResourcesResourceManager(GetType(Form))
 MeTreeView = New SystemWindowsFormsTreeView
 MeImageList = New SystemWindowsFormsImageList(ponents)
 MebtnOpenBrower = New SystemWindowsFormsButton
 MeSuspendLayout()
 
 TreeView
 
 MeTreeViewDock = SystemWindowsFormsDockStyleLeft
 MeTreeViewImageList = MeImageList
 MeTreeViewLocation = New SystemDrawingPoint( )
 MeTreeViewName = TreeView
 MeTreeViewSize = New SystemDrawingSize( )
 MeTreeViewTabIndex =
 
 ImageList
 
 MeImageListImageSize = New SystemDrawingSize( )
 MeImageListImageStream = CType(resourcesGetObject(ImageListImageStream) SystemWindowsFormsImageListStreamer)
 MeImageListTransparentColor = SystemDrawingColorTransparent
 
 btnOpenBrower
 
 MebtnOpenBrowerLocation = New SystemDrawingPoint( )
 MebtnOpenBrowerName = btnOpenBrower
 MebtnOpenBrowerSize = New SystemDrawingSize( )
 MebtnOpenBrowerTabIndex =
 MebtnOpenBrowerText = Open Brower
 
 Form
 
 MeAutoScaleBaseSize = New SystemDrawingSize( )
 MeClientSize = New SystemDrawingSize( )
 MeControlsAdd(MebtnOpenBrower)
 MeControlsAdd(MeTreeView)
 MeName = Form
 MeText = Form
 MeResumeLayout(False)

End Sub

#End Region
Private Sub OpenBrower()
 Dim f As New FolderBrowserDialog
 If fShowDialog = DialogResultOK Then
  MeTreeViewNodesClear()
  MeSuspendLayout()
  Dim nodeTemp As TreeNode = CType(MeInvoke(New AddTreeNodeHandler(AddressOf MeAddFileToTreeview) New Object() {fSelectedPath}) TreeNode)
  MeAddFile(New DirectoryInfo(fSelectedPath) nodeTemp)
  MeResumeLayout(False)
 End If
End Sub

Private Sub AddFile(ByVal dirPath As DirectoryInfo ByVal node As TreeNode)
 Dim tempHandler As New AddFileHandler(AddressOf MeAddFileToTreeNode)
 For Each dir As DirectoryInfo In dirPathGetDirectories()
  Dim nodeTemp As TreeNode = CType(MeInvoke(tempHandler New Object() {dirName CLASSIODirectory node}) TreeNode)
  MeAddFile(dir nodeTemp)
 Next
 For Each file As FileInfo In dirPathGetFiles(*dll)
  MeInvoke(tempHandler New Object() {fileName CLASSIOFile 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)
 nodeTempImageIndex = CInt(cls)
 nodeNodesAdd(nodeTemp)
 nodeTempSelectedImageIndex = nodeTempImageIndex
 Return nodeTemp
End Function

Private Function AddFileToTreeview(ByVal str As String) As TreeNode
 Dim nodeTemp As New TreeNode(str)
 MeTreeViewNodesAdd(nodeTemp)
 Return nodeTemp
 nodeTempSelectedImageIndex = CInt(CLASSIODirectory)
End Function

Private Sub btnOpenBrower_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles btnOpenBrowerClick
 多線程
 Dim t As ThreadingThread = New ThreadingThread(AddressOf MeOpenBrower)
 tStart()
 
 單線程
 MeOpenBrower()
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
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.