Click here to Skip to main content
15,891,253 members

Adding treenode using background worker

AlmirM asked:

Open original thread
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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