近段时间在自学WPF,是一个完全不懂WPF的菜鸟,对于在线程中修改UI控件使用委托做一个记录,给自已以后查询也给需要的参考:

界面只放一个RichTextBox,在窗体启动时开起两个线程,调用两个函数,每隔1秒写一次当前时间

一 界面XAML如下:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<Grid>
<ScrollViewer>
<RichTextBox HorizontalAlignment="Stretch" Margin="" Name="richTextBox1" VerticalAlignment="Stretch" />
</ScrollViewer>
</Grid>
</Window>

二 在界面启动时开启两个线程:

         private void Window_Loaded(object sender, RoutedEventArgs e)
{
//创建线程1
Thread t1 = new Thread(new ThreadStart(T1));
t1.Start(); //创建线程2
Thread t2 = new Thread(new ThreadStart(T2));
t2.Start();
}

三 线程调用函数:

        /// <summary>
/// 线程1调用函数
/// add by
/// </summary>
private void T1()
{
while (true)
{
Thread.Sleep(TimeSpan.FromSeconds());
ShowMsg(string.Format("T1 {0}", System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")));
}
}
/// <summary>
/// 线程2调用函数
/// add by
/// </summary>
private void T2()
{
while (true)
{
Thread.Sleep(TimeSpan.FromSeconds());
ShowMsg(string.Format("T2 {0}", System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")));
}
}

三 写前端函数:

        private void ShowMsg(string sMsg)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() {
richTextBox1.AppendText(string.Format("{0} \r\n",sMsg));
});
}

最新文章

  1. 为你的网站或App提供免费Https支持
  2. 11、java中的模板方法设计模式
  3. 看到shape文件可以加载到GOOGLE EARTH上的方法,有空可以试试
  4. shell学习
  5. 《2016ThoughtWorks技术雷达峰会----js爆炸下的技术选型》
  6. python生成汉字图片字库
  7. LA 3401 - Colored Cubes
  8. Linux中__init、__devinit等内核优化宏【转】
  9. win设置壁纸
  10. Inf2Cat, signability test failed.
  11. HW4.4
  12. oracle DG 主备切换语句整理
  13. Hbase Region Server 启动失败
  14. 阿里云部署mongdb(CentOS)
  15. shell高级特性-4
  16. 虹软2.0 离线人脸识别 Android 开发 Demo
  17. 升级node后还是原来版本问题
  18. 使用hadoop平台运行Apriori算法
  19. jQuery+html+css-自己写的分页
  20. DC-学习

热门文章

  1. vue打包遇到的坑
  2. Robot Framework学习笔记
  3. 定时任务框架Quartz-(一)Quartz入门与Demo搭建
  4. SP283 NAPTIME - Naptime
  5. adi i2s 提供的axi_lite接口说明
  6. 十 LVS 负载均衡
  7. openwrt查看flash、RAM、CPU信息
  8. 《剑指offer》翻转单词顺序列
  9. 敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
  10. Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required