分享
 
 
 

多线程填写treeview控件(vb.net)

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

功能:把选定文件夹下的dll、子文件夹和子文件夹中的dll填写到窗体的treeview控件上。

缺点:窗体简单,代码有些混乱,线程之间的调用可能没有到最优化。

实现:imagelist中的图片需要自己重新定义。

多线程的优点:可以及时响应用户的操作,不至于产生“未响应”的尴尬;设计合理(结合需求)的话,可使软件的性能提高。

多线程的编写:结构合理、注意同步。

以上是我对这个例程和多线程的个人体会,如有不妥之处,还请大家批评指正。

Imports System.IO

Public Class Form1

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 TreeView1 As System.Windows.Forms.TreeView

Friend WithEvents btnOpenBrower As System.Windows.Forms.Button

Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container

Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))

Me.TreeView1 = New System.Windows.Forms.TreeView

Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)

Me.btnOpenBrower = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'TreeView1

'

Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left

Me.TreeView1.ImageList = Me.ImageList1

Me.TreeView1.Location = New System.Drawing.Point(0, 0)

Me.TreeView1.Name = "TreeView1"

Me.TreeView1.Size = New System.Drawing.Size(376, 581)

Me.TreeView1.TabIndex = 0

'

'ImageList1

'

Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)

Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)

Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent

'

'btnOpenBrower

'

Me.btnOpenBrower.Location = New System.Drawing.Point(408, 64)

Me.btnOpenBrower.Name = "btnOpenBrower"

Me.btnOpenBrower.Size = New System.Drawing.Size(96, 23)

Me.btnOpenBrower.TabIndex = 1

Me.btnOpenBrower.Text = "Open Brower"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

Me.ClientSize = New System.Drawing.Size(536, 581)

Me.Controls.Add(Me.btnOpenBrower)

Me.Controls.Add(Me.TreeView1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub OpenBrower()

Dim f As New FolderBrowserDialog

If f.ShowDialog = DialogResult.OK Then

Me.TreeView1.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.TreeView1.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 = 1

Directory = 0

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有