M
mr_doles
Let me preface this by saying I am not a programmer, I write code to
make my like easier. So at the end of the day when I press the button
as long as 1 + 1 = 2, I don't care how ugly the code is. Now I am
writing some code for someone else and I have a window that takes
forever to open. I know that it is performing 3 large tasks in the
onLoad event. It works great just takes a while to load, any
optimization tips would be appreciated (no laughing at the code
because it does work).
We have a 3rd party app that sucks in info from all our computers and
puts it in a SQL database. One thing we pull in is uptime.exe
(support.microsoft.com/kb/232243) information. What I look for is
STOP errors and when they occurred and present them in a tree view
with the computer name as the parent node and each date as the child
node.
Here are the three steps (all in the onLoad event):
Variables:
Dim lo_sql_command As New SqlCommand()
Dim dsUptime As New DataSet()
Dim da As New SqlDataAdapter(lo_sql_command)
Dim s_line As String
Dim la_bluescreen As New ArrayList
1) Connect to SQL run query to get station name (string) and log file
(text) and put them in a DataSet
Dim lo_sql_command As New SqlCommand()
Dim dsUptime As New DataSet()
Dim da As New SqlDataAdapter(lo_sql_command)
Dim s_line As String
With lo_sql_command
.Connection = MyGlobals.trackitConnection
.CommandType = CommandType.Text
.CommandText = BuildBootQuery()
End With
Try
dsUptime.Clear()
da.Fill(dsUptime)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Exit Sub
Finally
lo_sql_command.Dispose()
da.Dispose()
End Try
2) Look at each rows text line by line for each match on STOP and
Bluescreen and add them to an array. Then add it to a hash table with
computer name as key and array as value.
For Each row As DataRow In dsUptime.Tables(0).Rows
Dim sr As New StringReader(row.Item(1).ToString)
la_bluescreen.Clear()
Try
'fill array
While sr.Peek <> -1
s_line = sr.ReadLine
If (Regex.IsMatch(s_line, "Bluescreen") = True)
And (Regex.IsMatch(s_line, "STOP") = True) Then
la_bluescreen.Add(Trim(s_line))
End If
End While
'Add to hastable
If (la_bluescreen.Count <> 0) Then
lh_bluescreens.Add(row.Item(0).ToString,
la_bluescreen)
End If
Catch ex As Exception
Throw ex
Finally
sr.Dispose()
dsUptime.Dispose()
End Try
Next
3)Create the tree view based on the hashtable and sort them
alphabetically
' Suppress repainting the TreeView until all the objects have
been created.
TreeView1.BeginUpdate()
TreeView1.Nodes.Clear()
' Add a root TreeNode for each Computer in the ArrayList -
Append the count
Dim nodecount As Integer = 0
For Each de As DictionaryEntry In lh_bluescreens
TreeView1.Nodes.Add(de.Key.ToString & " - {" &
DirectCast(de.Value, ArrayList).Count & "}")
' Add a child TreeNode for each stop error for current
computer.
Dim value As String
For Each value In DirectCast(de.Value, ArrayList)
TreeView1.Nodes(nodecount).Nodes.Add(value.ToString)
Next value
nodecount += 1
Next de
TreeView1.Sort()
' Begin repainting the TreeView.
TreeView1.EndUpdate()
'Cleanup
la_bluescreen = Nothing
lh_bluescreens = Nothing
make my like easier. So at the end of the day when I press the button
as long as 1 + 1 = 2, I don't care how ugly the code is. Now I am
writing some code for someone else and I have a window that takes
forever to open. I know that it is performing 3 large tasks in the
onLoad event. It works great just takes a while to load, any
optimization tips would be appreciated (no laughing at the code
because it does work).
We have a 3rd party app that sucks in info from all our computers and
puts it in a SQL database. One thing we pull in is uptime.exe
(support.microsoft.com/kb/232243) information. What I look for is
STOP errors and when they occurred and present them in a tree view
with the computer name as the parent node and each date as the child
node.
Here are the three steps (all in the onLoad event):
Variables:
Dim lo_sql_command As New SqlCommand()
Dim dsUptime As New DataSet()
Dim da As New SqlDataAdapter(lo_sql_command)
Dim s_line As String
Dim la_bluescreen As New ArrayList
1) Connect to SQL run query to get station name (string) and log file
(text) and put them in a DataSet
Dim lo_sql_command As New SqlCommand()
Dim dsUptime As New DataSet()
Dim da As New SqlDataAdapter(lo_sql_command)
Dim s_line As String
With lo_sql_command
.Connection = MyGlobals.trackitConnection
.CommandType = CommandType.Text
.CommandText = BuildBootQuery()
End With
Try
dsUptime.Clear()
da.Fill(dsUptime)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Exit Sub
Finally
lo_sql_command.Dispose()
da.Dispose()
End Try
2) Look at each rows text line by line for each match on STOP and
Bluescreen and add them to an array. Then add it to a hash table with
computer name as key and array as value.
For Each row As DataRow In dsUptime.Tables(0).Rows
Dim sr As New StringReader(row.Item(1).ToString)
la_bluescreen.Clear()
Try
'fill array
While sr.Peek <> -1
s_line = sr.ReadLine
If (Regex.IsMatch(s_line, "Bluescreen") = True)
And (Regex.IsMatch(s_line, "STOP") = True) Then
la_bluescreen.Add(Trim(s_line))
End If
End While
'Add to hastable
If (la_bluescreen.Count <> 0) Then
lh_bluescreens.Add(row.Item(0).ToString,
la_bluescreen)
End If
Catch ex As Exception
Throw ex
Finally
sr.Dispose()
dsUptime.Dispose()
End Try
Next
3)Create the tree view based on the hashtable and sort them
alphabetically
' Suppress repainting the TreeView until all the objects have
been created.
TreeView1.BeginUpdate()
TreeView1.Nodes.Clear()
' Add a root TreeNode for each Computer in the ArrayList -
Append the count
Dim nodecount As Integer = 0
For Each de As DictionaryEntry In lh_bluescreens
TreeView1.Nodes.Add(de.Key.ToString & " - {" &
DirectCast(de.Value, ArrayList).Count & "}")
' Add a child TreeNode for each stop error for current
computer.
Dim value As String
For Each value In DirectCast(de.Value, ArrayList)
TreeView1.Nodes(nodecount).Nodes.Add(value.ToString)
Next value
nodecount += 1
Next de
TreeView1.Sort()
' Begin repainting the TreeView.
TreeView1.EndUpdate()
'Cleanup
la_bluescreen = Nothing
lh_bluescreens = Nothing