如何使Dispatcher.BeginInvoke在VB.net中在后台运行? [英] How to make Dispatcher.BeginInvoke run in background in VB.net?

查看:163
本文介绍了如何使Dispatcher.BeginInvoke在VB.net中在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在后台运行应用程序的某些部分,并允许用户在子程序在后台运行时更新UI.我搜索了一下,发现在WPF中我应该使用Dispatcher.问题是,即使当我使用调度程序时,我的GUI仍无法使用,直到所有子程序都将完成.我在此处附加了代码,因此您可以更好地理解我的意思. 例如,在此代码中,当用户运行应用程序时,系统应运行一个新线程,该线程将更改第一个文本框的文本,而使用时可以更新第二个文本框的文本. 我想知道我是否做对了.有人可以帮我吗?

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?

<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

结束班级

<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

推荐答案

应使用分派器从单独的线程更新UI对象,它实际上并不会为您生成线程.如果使用的是.NET 4.0或更高版本,则可以使用TPL库生成线程,执行工作,然后通过调度程序从后台线程更新UI对象.

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.

Task.Factory.StartNew(Sub() DoBackgroundWork())

Task.Factory.StartNew(Sub() DoBackgroundWork())

然后,每当您要更新UI时,在DoBackgroundWork内...

Then, inside DoBackgroundWork whenever you want to update your UI...

Dispatcher.BeginInvoke(Sub() txtBox1.Text = "End")

Dispatcher.BeginInvoke(Sub() txtBox1.Text = "End")

这篇关于如何使Dispatcher.BeginInvoke在VB.net中在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆