Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
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 21-Nov-12 4:48am
AlmirM666

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
AlmirM at 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 at 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)

  Print Answers RSS
0 OriginalGriff 464
1 Sergey Alexandrovich Kryukov 429
2 Maciej Los 285
3 CPallini 180
4 Peter Leow 170
0 Sergey Alexandrovich Kryukov 6,971
1 OriginalGriff 5,532
2 Peter Leow 3,997
3 Mika Wendelius 2,850
4 CHill60 2,808


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100