【发布时间】:2010-09-20 11:34:33
【问题描述】:
好的,现在我可以使用 2 种技术来启动我的线程:Dispatcher 和 BackgroundWorker。
调度员:
' I launch the asynchronous process
Dim a As New Action(AddressOf operazioneLunga)
a.BeginInvoke(Nothing, Nothing)
' I update the GUI passing some parameters
Dim a As New Action(Of Integer, String)(AddressOf aggiorna_UI)
Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, a, 5, "pippo")
后台工作人员:
Private bw As BackgroundWorker = Nothing
Private Sub initial()
bw = New BackgroundWorker
AddHandler bw.DoWork, AddressOf longOp
AddHandler bw.RunWorkerCompleted, AddressOf endBGW
bw.RunWorkerAsync ()
End Sub
Private Sub longOp(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim l As List(Of miaClasse2) = <Long Operation ...>
e.Result = l
End Sub
Private Sub endBGW(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
Dim l As List(Of miaClasse2) = e.Result
Dim be As BindingExpression = BindingOperations.GetBindingExpression(mioDatagrid, DataGrid.ItemsSourceProperty)
Dim m As miaClasse1 = DirectCast(be.DataItem, miaClasse1)
m.GetData (l)
mioDatagrid.UpdateLayout()
mioDatagrid.ScrollIntoView (mioDatagrid.Items(0))
RemoveHandler bw.DoWork, AddressOf massiccia
RemoveHandler bw.RunWorkerCompleted, AddressOf fineBGW
bw.Dispose()
End Sub
我不知道什么更好,但我想我会使用 BackgroundWorker,因为我想还有其他关于 Dispatcher 的争论我必须知道而且我觉得不安全。
皮莱吉
我之前的帖子:
大家好!
我的应用程序在 WPF / Vb 框架 3.5 SP1 中。我需要在异步线程上执行一些方法。我是这样认识的:
Private Delegate Sub dMassiccia()
Private Delegate Sub dAggiornaUI()
Private Sub iniziale()
Dim massicciaTemp As New dMassiccia(AddressOf massiccia)
massicciaTemp.BeginInvoke(Nothing, Nothing)
End Sub
Private Sub massiccia()
'long operations...
Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _
New dAggiornaUI(AddressOf aggiornaUI))
End Sub
Private Sub aggiornaUI()
'update the UI...
End Sub
但是通过这种方式,我必须为我想在异步线程上启动的每个方法声明一个委托,这非常不舒服。我有很多方法可以通过这种方式启动。我知道有匿名代表,但我不知道在这种情况下如何使用它们。 你能帮助我吗? 皮莱吉
附言。其他信息:此时我不需要查找在异步线程中启动的进程的状态。长操作是对 Web 服务的一些请求,每次可能需要几秒钟。线程的数量没有问题,因为我限制了用户启动新线程的可能性,直到其中一个线程完成。除其他原因外,我需要异步线程,因为我不会阻止应用程序,我想用用户控件替换鼠标光标等。
【问题讨论】:
标签: wpf vb.net visual-studio-2008 asynchronous anonymous-types