Private Sub ToolStripButton11_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton11.Click Dim clbItems As New List(Of String) ' Make data ready for the thread For Each item As TreeNode In TreeView4.Nodes clbItems.Add(item.Text) Next If BackgroundWorker1.IsBusy Then ToolStripButton11.Enabled = False ToolStripStatusLabel1.Text = "Canceling..." BackgroundWorker1.CancelAsync() Else ToolStripButton11.Text = "Cancel" ToolStripStatusLabel1.Text = "Running..." BackgroundWorker1.RunWorkerAsync(clbItems) End If End Sub Private Sub BackgroundWorker1_DoWork_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim bwAsync As BackgroundWorker = TryCast(sender, BackgroundWorker) Dim clbItems As List(Of String) = CType(e.Argument, List(Of String)) Dim item As String ' These 5 line should not have to be run each time through the loop Dim con As New SqlConnection(My.Settings.SQLServerConn) con.Open() Dim cmd As New SqlCommand() cmd.Connection = con cmd.CommandTimeout = 0 cmd.CommandType = CommandType.StoredProcedure For Each item In clbItems ' Send string item to the ProgressChanged event to update GUI safely bwAsync.ReportProgress(0, item) If bwAsync.CancellationPending Then Thread.Sleep(1200) e.Cancel = True Return End If cmd.CommandText = item 'Thread.Sleep(1200) cmd.ExecuteNonQuery() ' HERE I WOULD LIKE TO ADD NODE TO ITEM (WHICH REPRESENTS SP) Next ' Don't forget to close the connection con.Close() End Sub Private Sub BackgroundWorker1_RunWorkerCompleted_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted ToolStripButton11.Text = "Run Stored procedures" ToolStripButton11.Enabled = True ' Check to see if an error occured in the ' background process. If e.[Error] IsNot Nothing Then MessageBox.Show(e.[Error].Message) Return End If ' Check to see if the background process was cancelled. If e.Cancelled Then ToolStripStatusLabel1.Text = "Cancelled..." Else ' Everything completed normally. ' process the response using e.Result ToolStripStatusLabel1.Text = "Completed..." End If End Sub Private Sub BackgroundWorker1_ProgressChanged_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged ToolStripStatusLabel1.Text = e.UserState.ToString End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)