在WPF的用户线程中更新UI界面
2007-01-08 17:39
323 查看
WPF中UI线程队列由Dispatcher来管理和调度,所以当用户线程中更新UI时,必须通过Dispatche来调度,下面这个小例子将给用户展示如何在用户线程中更新当前的时间.
前台的XAML代码如下:
<Windowx:Class="ThreadInvoke.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ThreadInvoke"Height="300"Width="300"
>
<StackPanelOrientation="Vertical">
<StackPanelOrientation="Horizontal">
<ButtonContent="Ok"Click="okClick"Width="50"/>
<ButtonContent="Stop"Click="stopClick"Width="50"/>
</StackPanel>
<TextBoxName="timeText"></TextBox>
</StackPanel>
</Window>
后台的主要代码如下:
//申明一个代理用于想UI更新时间
private delegate void DelegateSetCurrentTime();
//申明一个变量,用于停止时间的跳动
private bool stopFlag = false;
//处理开始和结束事件
private void okClick(object sender,RoutedEventArgs args)
{
stopFlag = false;
Thread thread = new Thread(new ThreadStart(refreshTime));
thread.Start();
}
private void stopClick(object sender, RoutedEventArgs args)
{
stopFlag = true;
}
//用户线程的实现函数
private void refreshTime()
{
while (!stopFlag)
{
//向UI界面更新时钟显示 Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.SystemIdle, new DelegateSetCurrentTime(setCurrentTime));
}
}
private void setCurrentTime()
{
String currentTime = System.DateTime.Now.ToString();
timeText.Text = currentTime;
}
2007-1-8 Paul.Peng
前台的XAML代码如下:
<Windowx:Class="ThreadInvoke.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ThreadInvoke"Height="300"Width="300"
>
<StackPanelOrientation="Vertical">
<StackPanelOrientation="Horizontal">
<ButtonContent="Ok"Click="okClick"Width="50"/>
<ButtonContent="Stop"Click="stopClick"Width="50"/>
</StackPanel>
<TextBoxName="timeText"></TextBox>
</StackPanel>
</Window>
后台的主要代码如下:
//申明一个代理用于想UI更新时间
private delegate void DelegateSetCurrentTime();
//申明一个变量,用于停止时间的跳动
private bool stopFlag = false;
//处理开始和结束事件
private void okClick(object sender,RoutedEventArgs args)
{
stopFlag = false;
Thread thread = new Thread(new ThreadStart(refreshTime));
thread.Start();
}
private void stopClick(object sender, RoutedEventArgs args)
{
stopFlag = true;
}
//用户线程的实现函数
private void refreshTime()
{
while (!stopFlag)
{
//向UI界面更新时钟显示 Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.SystemIdle, new DelegateSetCurrentTime(setCurrentTime));
}
}
private void setCurrentTime()
{
String currentTime = System.DateTime.Now.ToString();
timeText.Text = currentTime;
}
2007-1-8 Paul.Peng
相关文章推荐
- 在wpf的用户线程中更新ui界面
- 在WPF的用户线程中更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- PHP,操作多个用户,多个线程的session,实现用户登陆状态session值的自动更新
- 在分线程更新主线程的Ui界面方法
- WPF后台线程更新UI
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- WPF:如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- WPF跨线程更新UI的3种方法
- WPF -- 从其他线程中修改更新控件
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- WPF线程中被修改后DataGrid数据不能及时更新的问题
- PHP,操作多个用户,多个线程的session,实现用户登陆状态session值的自动更新
- android线程更新UI界面
- Thread+Handler实现非UI线程更新UI界面
- 【WPF学习】WPF、WinForm(C#)多线程编程并更新界面(UI)/子线程更新主界面方式
- WPF:如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面