Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I use backgroundWorker in order to execute stored procedures on SQL Server.
It works OK, but since all stored procedures in GUI are shown in treeView as root nodes I would like to add child node to each executed sp after the execution of the same is completed (for eg. I would like to add node named "OK - done").
Here is my vb code I use to run procedures but try to add nodes with no success. I marked position where I would like to do this marked as ' HERE I WOULD LIKE TO ADD NODE TO ITEM (WHICH REPRESENTS SP)

Thanks a lot for help
A

VB
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
Posted

1 solution

I don't think it's a good idea to do what you are trying to do. The basic idea of the BackgroundWorker is that all of the code in the DoWork event is being performed on a separate thread than the GUI controls and related code are on. If you add code in the DoWork event that accesses a TreeNode that was declared on your form you could get a cross-threading error. (You don't specify why you have failed thus far to get your idea to work, had you already tried and got an error? If so, please click the Improve Question link and give us the details of that).

If you really want what you are asking for, you may have to research some more complicated thread handling methods.

Hope this helps.
 
Share this answer
 
Comments
AlmirM 22-Nov-12 2:44am    
Thanks for reply. Eventually it turned out as very simple task by adding tread in Progress changed event. It works as charm
Almir
Kschuler 26-Nov-12 9:19am    
Cool. You should post your solution here so others who have a similar issue can see how you worked it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900