vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

VB.NET中采用多线程填写treeview控件


发布日期:2021年11月14日
 
VB.NET中采用多线程填写treeview控件
功能把选定文件夹下的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

上一篇:用VB.net2008编写幻灯片程序

下一篇:一步一步在VB.NET中使用抽象类