新线程仍在阻止UI线程 [英] New Thread is still blocking UI-Thread
问题描述
单击按钮时,我将启动一个单独的主题,该主题将填充网格并通过Web浏览器控件执行某些操作.但是当单击按钮时,新线程似乎并没有分开,导致UI冻结,直到完成新线程为止.
when a button is clicked i start a seperate thead which is population a grid and does something with a webbrowser-control. But when the button is click the new thread seems not to be seperate, cause the UI is freezing until the new thread is done.
代码在这里:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
FindCustomerLocation()
e.Handled = True
End Sub
Private Sub FindCustomerLocation()
Dim Findcontractor_Thread As New Thread(AddressOf FindContractor_ThreadExecute)
Findcontractor_Thread.Priority = ThreadPriority.AboveNormal
Findcontractor_Thread.Start()
End Sub
Delegate Sub FindContractorDelegate(ByVal igGrid As Infragistics.Windows.DataPresenter.XamDataGrid, ByVal webbrowser As Controls.WebBrowser)
Private Sub FindContractor_ThreadExecute()
Dim threadControls(1) As Object
threadControls(0) = Me.XamDataGrid1
threadControls(1) = Me.WebBrowserMap
Dim m As FindContractorDelegate = AddressOf FindContractor_WorkingThread
Me.Dispatcher.BeginInvoke(m, threadControls)
End Sub
Private Sub FindContractor_WorkingThread()
Mouse.OverrideCursor = Cursors.Wait
'Do something...
Mouse.OverrideCursor = Nothing
End Sub
我做错了什么?
谢谢, 尼尔斯
推荐答案
您可能会在做某事"中使用winforms控件.只能在UI线程中维护控件.
You propabldy working with the winforms controls in the "'do something". The controls can be mainupulated only in UI thread.
因此,"BeginInvoke"在UI线程中调用目标.因此,您创建了并行线程,但是整个想法又在UI线程中完成了.
So, the "BeginInvoke" call the target in ths UI thread. So you create te paralel thread, but the whole think is doing in UI thread again.
这篇关于新线程仍在阻止UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!