Vb.net ActionBlock完成。等待状态似乎挂起了整个程序
下面的程序按照我的意图工作,它演示了两个不同的ActionBlock使用两个不同的线程。但是,我读到的所有内容都说,我应该使用ActionBlock.Completion.Wait,以便让ActionBlock完成它的工作。当我将当前已注释的内容添加到此程序时,它将挂起 是因为我试图从UI线程启动ActionBlock吗?我是不是应该把这堆乱七八糟的东西放在一个后台工作人员那里Vb.net ActionBlock完成。等待状态似乎挂起了整个程序,vb.net,task-parallel-library,Vb.net,Task Parallel Library,下面的程序按照我的意图工作,它演示了两个不同的ActionBlock使用两个不同的线程。但是,我读到的所有内容都说,我应该使用ActionBlock.Completion.Wait,以便让ActionBlock完成它的工作。当我将当前已注释的内容添加到此程序时,它将挂起 是因为我试图从UI线程启动ActionBlock吗?我是不是应该把这堆乱七八糟的东西放在一个后台工作人员那里 Imports System.Data.SqlClient Imports System.Threading.Task
Imports System.Data.SqlClient
Imports System.Threading.Tasks.Dataflow
Imports System.Windows.Forms
Public Class Form1
Protected _theConn As SqlConnection
Protected _DataList As New List(Of UISyncer)
Protected _TableList As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
_theConn = New SqlConnection("Server=USFRNDCUSHING2\MSSQSERVER2012;Database=ActionTaskTest;User Id=sa;Password=Sa1234;")
_DataList.Add(New UISyncer("ActionTaskOne", txtParallel1))
_DataList.Add(New UISyncer("ActionTaskTwo", txtParallel2))
_TableList.Add("ActionTaskOne")
_TableList.Add("ActionTaskTwo")
End Sub
Private Function ReadFromTable(ByVal tableName As String) As DataSet
Dim ds As New DataSet()
Dim adapt As SqlDataAdapter = New SqlDataAdapter()
Dim selectString As String = "SELECT * FROM " & tableName
adapt.SelectCommand = New SqlCommand(selectString, _theConn)
_theConn.Open()
adapt.Fill(ds, tableName)
_theConn.Close()
Return ds
End Function
Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
txtOut1.ResetText()
DoTheWorkSimpler()
End Sub
Private Sub DoTheWorkSimpler()
For Each tableName As String In _TableList
Dim dsResult As DataSet = ReadFromTable(tableName)
DealWithDataSet(dsResult, 1, tableName)
Next
End Sub
Private Sub DealWithDataSet(ByRef dsResult1 As DataSet, ByVal maxDegreeOfParallelism As Integer, ByVal tableName As String)
Dim workerBlock = New ActionBlock(Of DataRow)(Function(dr) DoStuff(dr), New ExecutionDataflowBlockOptions() With {.MaxDegreeOfParallelism = maxDegreeOfParallelism})
For Each dr As DataRow In dsResult1.Tables(tableName).Rows
workerBlock.Post(dr)
Next
workerBlock.Complete()
'Here is my question, why does this hang my program
'workerBlock.Completion.Wait() 'This just seems to hang the whole danged thing.
End Sub
'This might want to be shared, if it wasn't writing to the text box
Private Function DoStuff(ByRef dr As DataRow)
Dim theString As String = dr("TaskDesc").ToString() & " on Thread: " & Threading.Thread.CurrentThread.ManagedThreadId & Environment.NewLine
AppendTextBox(theString)
Return Nothing
End Function
Public Sub AppendTextBox(value As String)
If InvokeRequired Then
Me.Invoke(New Action(Of String)(AddressOf AppendTextBox), New Object() {value})
Return
End If
txtOut1.Text += value
End Sub
End Class
不要使用Wait,尤其是在UI线程中。用等待代替。我可以用等待来做我想做的事。非常感谢。那么,我如何将评论标记为答案呢?你不能。你不应该,因为我没有付出任何努力。你应该添加你的答案,解释什么是错误的,以及你是如何解决的。如果你愿意,你可以把它变成社区维基。