wpf 如何让 Dispatcher.BeginInvoke 在 VB.net 后台运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19109424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Dispatcher.BeginInvoke run in background in VB.net?
提问by Saman
I need to run some part of application in background and allow user to update the UI while the sub is running in the background. I searched and I found out that in WPF I should use Dispatcher. Problem is even when I use dispatcher still my GUI is not usable till the all subs will finish. I attached a code here so you can have better perspective of what I mean. For example in this code when a user run the application, system should run a new thread that will change the text of first textbox while use can update the text of the second textbox. I am wondering if I am doing this right or not. Can anybody help me with this?
我需要在后台运行应用程序的某些部分,并允许用户在 sub 在后台运行时更新 UI。我搜索并发现在 WPF 中我应该使用 Dispatcher。问题是即使我使用调度程序,我的 GUI 仍然无法使用,直到所有潜艇都完成。我在这里附上了一个代码,这样你就可以更好地理解我的意思。例如在这段代码中,当用户运行应用程序时,系统应该运行一个新线程来更改第一个文本框的文本,而使用可以更新第二个文本框的文本。我想知道我这样做是否正确。有人可以帮我解决这个问题吗?
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:testDispacher"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<TextBlock>UCtextbox:</TextBlock>
<src:ToBeRunByDispacherUC x:Name="UC1" />
<TextBlock>Windowstxtbox:</TextBlock>
<TextBox x:Name="txtBox2" Width="100" Height="30"/>
</StackPanel>
</Grid>
Class Window1
Delegate Sub runSub()
Dim setTxt As runSub
Public Sub New()
InitializeComponent()
setTxt = AddressOf UC1.setTxtBox
End Sub
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
UC1.IsEnabled = False
Dispatcher.Invoke(setTxt, Windows.Threading.DispatcherPriority.Background)
End Sub
End Class
结束类
<UserControl x:Class="ToBeRunByDispacherUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBox x:Name="txtBox1" Width="100" Height="30"/>
</Grid>
Partial Public Class ToBeRunByDispacherUC
Public Sub setTxtBox()
Dim j As Integer = 0
For i As Integer = 0 To 10
j += 1
System.Threading.Thread.Sleep(1000)
Next
txtBox1.Text = "End"
Me.IsEnabled = True
End Sub
End Class
回答by Niko Carrizo
The dispatcher should be used to update UI objects from a separate thread, it does not actually spawn up the thread for you. If you are using .NET 4.0 or higher, you can use the TPL library to spawn your thread, do your work, then update your UI object via the dispatcher from the background thread.
调度程序应该用于从单独的线程更新 UI 对象,它实际上不会为您生成线程。如果您使用 .NET 4.0 或更高版本,您可以使用 TPL 库来生成您的线程,完成您的工作,然后通过后台线程的调度程序更新您的 UI 对象。
Task.Factory.StartNew(Sub() DoBackgroundWork())
Task.Factory.StartNew(Sub() DoBackgroundWork())
Then, inside DoBackgroundWork whenever you want to update your UI...
然后,每当您想更新 UI 时,在 DoBackgroundWork 中...
Dispatcher.BeginInvoke(Sub() txtBox1.Text = "End")
Dispatcher.BeginInvoke(Sub() txtBox1.Text = "End")
回答by boxsp
you can call to the general application.
你可以调用通用应用程序。
Application.Current.Dispatcher.Invoke()

