多线程填写treeview控件(vb.net)
2024-07-10 13:00:57
供稿:网友
注册会员,创建你的web开发资料库,功能:把选定文件夹下的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