我正在开发一个应用程序,能够在 3x3(即 9 个屏幕)视频墙上监控生产信息。我现在正在处理的屏幕集之一检索信息,然后格式化以在屏幕上显示。检索和格式化此数据大约需要 2 秒(只是粗略猜测,未实际测量)。因为它有 9 个屏幕,一个接一个,所以切换到这个屏幕集需要很长的时间。驱动此视频墙的 PC 有 8 个处理核心,因此当一个处理器忙着完成所有这些工作时,还有大量的处理能力处于闲置状态。
我的第一个想法是我需要使用多线程。不幸的是,我对这个概念很陌生。我确实只用过一次。我尝试创建一个BackgroundWorker 并让DoWork 例程生成我的UI。不幸的是,当我第一次尝试创建 UI 元素(Dim grLine as New Grid
)时,它崩溃了。我确实设法通过使用一个虚拟的 DoWork 例程并在 RunWorkerCompleted 例程中生成所有 UI 来解决这个问题。这确实允许我的空白窗口立即显示,但我生成的 UI 在全部渲染完成之前不会显示。
这是我正在尝试做的事情的一个非常干净的版本:
For i As Integer = 1 to 9
Dim win As New MyCustomWindow
win.DisplayScreen = i ' This function in MyCustomWindow sets the Bounds
win.MyShow({1, 2}) ' Sample args
Globals.VideoWall.Windows(i) = win
Next
MyCustomWindow 类:
Class MyCustomWindow
Public Sub MyShow(a() as Integer)
Me.Show() ' Has a "Loading..." TextBlock
Dim bw as New ComponentModel.BackgroundWorker
AddHandler bw.DoWork, AddressOf Generate_UI_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf Generate_UI_Complete
bw.RunWorkerAsync(a)
End Sub
Private Sub Generate_UI_DoWork((sender As Object, e As ComponentModel.DoWorkEventArgs)
' Pass our arguments to the Complete routine.
e.Result = e.Argument
End Sub
Private Sub Generate_OpsMarket_Complete(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs)
Dim IDs() as Integer
IDs = e.Result
Dim grLine As New Grid ' We crash here if this code is in DoWork instead of RunWorkerCompleted
For Each id As Integer In IDs
grLine.RowDefinitions.Add(New RowDefinition)
Dim txt as New TextBlock ' For a header
grLine.Children.Add(txt)
grLine.RowDefinitions.Add(New RowDefinition)
Dim MyCtrl as New MyCustomControl()
MyCustomControl.GetData(id)
grLine.Children.Add(MyCtrl.MyGrid)
txt.Text = MyCtrl.Header
Next
txLoading.Visibility = Visibility.Hidden
grRoot.Children.Add(grLine)
End Sub
End Class
我试图在代码中留下足够的细节,因此希望它能清楚地表明我想要完成的任务,但保持它足够小,以免让人不知所措。
编辑添加:
大部分工作发生在 MyCustomControl.GetData(id)
中...Sub 从 Web 服务器下载数据(JSON 格式),解析 JSON,然后生成行 (3) 和网格的列(30 或 31,具体取决于月份)并填写从 Web 服务器接收的数据。
最佳答案
正如您自己所注意到的,您当前的 BackgroundWorker
实现没有给您带来任何好处。
您的主要问题是您当前的代码/逻辑紧密依赖于 UI 控件。这对 UI 线程有严格的要求。因为创建/更新 UI 控件只能在 UI 线程上完成 - 这就是为什么在 BackgroundWorker.DoWork
处理程序中尝试创建/更新 UI 控件时出现异常。
建议分离并行检索监控信息的逻辑,然后您可以使用已格式化的数据在 UI 上创建/更新控件。
这是原始/伪示例
Class DataService
Public Function GetData(ids As Integer()) As YourData
' Get data from web service
' Validate and Format it to YourData type or List(Of Yourdata)
Return data
End Function
End Class
Class MyCustomWindow
Public Sub MyShow(a() as Integer)
Me.Show() ' Has a "Loading..." TextBlock
Dim bw as New ComponentModel.BackgroundWorker
AddHandler bw.DoWork, AddressOf Generate_UI_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf Generate_UI_Complete
bw.RunWorkerAsync(a)
End Sub
Private Sub Generate_UI_DoWork((sender As Object, e As ComponentModel.DoWorkEventArgs)
Dim service As New DataService()
Dim data = service.GetData(e.Argument)
e.Result = data
End Sub
Private Sub Generate_OpsMarket_Complete(sender As Object,
e As ComponentModel.RunWorkerCompletedEventArgs)
Dim data As Yourdata = DirectCast(e.Result, YourData)
'Update UI controls with already formatted data
End Sub
End Class
Sub 上的更新从网络服务器下载数据
在这种情况下,您根本不需要多线程/并行。因为你的加载时间就是等待响应的时间。在这种情况下,我的建议是使用 async/await
方法,该方法将在您等待 Web 服务响应时释放 UI 线程(使其响应)。
Class DataService
Public Async Function GetDataAsync(ids As Integer()) As Task(Of YourData)
Using client As HttpClient = New HttpClient()
Dim response As HttpResponseMessage = Await client.GetAsync(yourUrl)
If response.IsSuccessStatusCode = True Then
Return Await response.Content.ReadAsAsync<YourData>()
End If
End Using
End Function
End Class
然后在 View 中你不需要BackgroundWorker
Class MyCustomWindow
Public Async Sub MyShow(a() as Integer) As Task
Me.Show() ' Has a "Loading..." TextBlock
Dim service As New DataService()
Dim data As YourData = Await service.GetDataAsync(a)
UpdateControlsWithData(data)
End Sub
Private Sub UpdateControlsWithData(data As YourData)
' Update controls with received data
End Sub
End Class
关于wpf - vb.Net:在BackgroundWorker中创建UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554945/