donjkurian
Member
- Joined
- Oct 6, 2009
- Messages
- 9
- Programming Experience
- 1-3
Hi,
Please help me introduce multithreading into my code so that the UI is more responsive.
Here is what i've done:
I have a program which searches the entire registry for a keyword and adds the matching keys/values into a listview. The UI becomes non responsive if i attempt to move the form or click on any of the other buttons in the form, while the search is going on.
Can you guys tell me how i can restructure my code so that the long searching operation is in a different thread and all the UI stuffs remain in the UI thread itself.
I have googled enough and got so many links, but ive not been able to implement it so far with my code. It confuses me a lot.
Im including my code below:
Imports Microsoft.Win32
Public Class Form1
Dim count As Integer
Sub FullRegSearch(ByVal root As RegistryKey, ByVal searchKey As String)
Dim val As String = Nothing
Try
For Each keyname As String In root.GetSubKeyNames
TextBox2.Refresh()
TextBox2.Text = "Searching :" + root.ToString
TextBox3.Text = count.ToString + " items found"
Try
Using key As RegistryKey = root.OpenSubKey(keyname)
Try
If keyname.Contains(searchKey) Then
ListView1.Refresh()
ListView1.Items.Add("Key :" + key.Name)
ListView1.Refresh()
count = count + 1
End If
Catch ex As Exception
End Try
Try
For Each valuename As String In key.GetValueNames
val = (key.GetValue(valuename))
Try
If val.ToString.Contains(searchKey) Then
ListView1.Refresh()
ListView1.Items.Add("Key :" + key.Name)
ListView1.Refresh()
ListView1.Items.Add("Value :" + val)
ListView1.Refresh()
count = count + 2
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
FullRegSearch(key, searchKey)
End Using
Catch ex As Exception
End Try
TextBox3.Refresh()
Next
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListView1.Items.Clear()
'#check if search keyword is empty in textbox1
If TextBox1.Text = "" Then
MsgBox("Please enter the search keyword", MsgBoxStyle.Information)
Exit Sub
End If
'#If checkbox4 is checked (HKEY_CLASSES_ROOT)
If CheckBox4.Checked = True Then
FullRegSearch(Registry.ClassesRoot, TextBox1.Text)
End If
'#If checkbox5 is checked (HKEY_CURRENT_USER)
If CheckBox5.Checked = True Then
FullRegSearch(Registry.CurrentUser, TextBox1.Text)
End If
'#If checkbox6 is checked (HKEY_LOCAL_MACHINE)
If CheckBox6.Checked = True Then
FullRegSearch(Registry.LocalMachine, TextBox1.Text)
End If
'#If checkbox7 is checked (HKEY_USERS)
If CheckBox7.Checked = True Then
FullRegSearch(Registry.Users, TextBox1.Text)
End If
'#If checkbox8 is checked (HKEY_CURRENT_CONFIG)
If CheckBox8.Checked = True Then
FullRegSearch(Registry.CurrentConfig, TextBox1.Text)
End If
'After search is complete
If ListView1.Items.Count = 0 Then
MsgBox("Search completed ! No matching keys/values found in registry.", MsgBoxStyle.Information, "PristineReg")
count = Nothing
TextBox2.Text = ""
TextBox3.Text = ""
Exit Sub
End If
MsgBox("Search completed !", MsgBoxStyle.Information, "PristineReg")
count = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.MaximizeBox = False
ListView1.CheckBoxes = True
CheckBox4.Checked = True
CheckBox5.Checked = True
CheckBox6.Checked = True
CheckBox7.Checked = True
CheckBox8.Checked = True
End Sub
End Class
Thanks in advance,
donjkurian
Please help me introduce multithreading into my code so that the UI is more responsive.
Here is what i've done:
I have a program which searches the entire registry for a keyword and adds the matching keys/values into a listview. The UI becomes non responsive if i attempt to move the form or click on any of the other buttons in the form, while the search is going on.
Can you guys tell me how i can restructure my code so that the long searching operation is in a different thread and all the UI stuffs remain in the UI thread itself.
I have googled enough and got so many links, but ive not been able to implement it so far with my code. It confuses me a lot.
Im including my code below:
Imports Microsoft.Win32
Public Class Form1
Dim count As Integer
Sub FullRegSearch(ByVal root As RegistryKey, ByVal searchKey As String)
Dim val As String = Nothing
Try
For Each keyname As String In root.GetSubKeyNames
TextBox2.Refresh()
TextBox2.Text = "Searching :" + root.ToString
TextBox3.Text = count.ToString + " items found"
Try
Using key As RegistryKey = root.OpenSubKey(keyname)
Try
If keyname.Contains(searchKey) Then
ListView1.Refresh()
ListView1.Items.Add("Key :" + key.Name)
ListView1.Refresh()
count = count + 1
End If
Catch ex As Exception
End Try
Try
For Each valuename As String In key.GetValueNames
val = (key.GetValue(valuename))
Try
If val.ToString.Contains(searchKey) Then
ListView1.Refresh()
ListView1.Items.Add("Key :" + key.Name)
ListView1.Refresh()
ListView1.Items.Add("Value :" + val)
ListView1.Refresh()
count = count + 2
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
FullRegSearch(key, searchKey)
End Using
Catch ex As Exception
End Try
TextBox3.Refresh()
Next
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListView1.Items.Clear()
'#check if search keyword is empty in textbox1
If TextBox1.Text = "" Then
MsgBox("Please enter the search keyword", MsgBoxStyle.Information)
Exit Sub
End If
'#If checkbox4 is checked (HKEY_CLASSES_ROOT)
If CheckBox4.Checked = True Then
FullRegSearch(Registry.ClassesRoot, TextBox1.Text)
End If
'#If checkbox5 is checked (HKEY_CURRENT_USER)
If CheckBox5.Checked = True Then
FullRegSearch(Registry.CurrentUser, TextBox1.Text)
End If
'#If checkbox6 is checked (HKEY_LOCAL_MACHINE)
If CheckBox6.Checked = True Then
FullRegSearch(Registry.LocalMachine, TextBox1.Text)
End If
'#If checkbox7 is checked (HKEY_USERS)
If CheckBox7.Checked = True Then
FullRegSearch(Registry.Users, TextBox1.Text)
End If
'#If checkbox8 is checked (HKEY_CURRENT_CONFIG)
If CheckBox8.Checked = True Then
FullRegSearch(Registry.CurrentConfig, TextBox1.Text)
End If
'After search is complete
If ListView1.Items.Count = 0 Then
MsgBox("Search completed ! No matching keys/values found in registry.", MsgBoxStyle.Information, "PristineReg")
count = Nothing
TextBox2.Text = ""
TextBox3.Text = ""
Exit Sub
End If
MsgBox("Search completed !", MsgBoxStyle.Information, "PristineReg")
count = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.MaximizeBox = False
ListView1.CheckBoxes = True
CheckBox4.Checked = True
CheckBox5.Checked = True
CheckBox6.Checked = True
CheckBox7.Checked = True
CheckBox8.Checked = True
End Sub
End Class
Thanks in advance,
donjkurian