Multithreading fill out the TreeView control (vb.net)
Source: Internet
Author: User
treeview| Multithreading | Control Features: Fills the DLL, subfolders, and subfolders in the selected folder into the TreeView control on the form.
Disadvantages: The form is simple, the code is somewhat confusing, and calls between threads may not be optimized.
Implementation: The picture in ImageList needs to be redefined itself.
The advantages of Multithreading: can respond to the user's operation in a timely manner, not to produce "no response" embarrassment, design reasonable (combination of requirements), can improve the performance of the software.
Multi-threaded writing: reasonable structure and attention to synchronization.
The above is my personal experience of this routine and multithreading, if there are irregularities, but also please criticize correct.
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
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), 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 all 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
' Multithreading
Dim T as Threading.thread = New threading.thread (AddressOf me.openbrower)
T.start ()
' Single thread
' 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
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service