使用后台工作器添加treenode [英] Adding treenode using background worker

查看:68
本文介绍了使用后台工作器添加treenode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用backgroundWorker以便在SQL Server上执行存储过程.
它可以正常工作,但是由于GUI中的所有存储过程都在treeView中显示为根节点,因此我想在每个已执行的sp执行完毕后将其添加到每个已执行的sp中(例如,我想添加名为"OK"的节点) -完成").
这是我用来运行过程的vb代码,但尝试添加节点没有成功.我将要执行此操作的位置标记为''我愿意在此添加项目项(代表SP)

非常感谢您的帮助
A

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

推荐答案

我认为做自己想做的事不是一个好主意. BackgroundWorker的基本思想是DoWork事件中的所有代码都在单独的线程上执行,而不是在GUI控件和相关代码上执行.如果在DoWork事件中添加代码以访问在表单上声明的TreeNode,则可能会遇到跨线程错误. (您尚未指定为什么到目前为止无法使您的想法付诸实践,您是否已经尝试过并遇到错误?如果是这样,请单击改进问题"链接并向我们提供详细信息).

如果您真的想要您的要求,则可能必须研究一些更复杂的线程处理方法.

希望这会有所帮助.
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.


这篇关于使用后台工作器添加treenode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆