C# WPF 使用委托修改UI控件
2024-10-09 22:10:42
近段时间在自学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));
});
}
最新文章
- 为你的网站或App提供免费Https支持
- 11、java中的模板方法设计模式
- 看到shape文件可以加载到GOOGLE EARTH上的方法,有空可以试试
- shell学习
- 《2016ThoughtWorks技术雷达峰会----js爆炸下的技术选型》
- python生成汉字图片字库
- LA 3401 - Colored Cubes
- Linux中__init、__devinit等内核优化宏【转】
- win设置壁纸
- Inf2Cat, signability test failed.
- HW4.4
- oracle DG 主备切换语句整理
- Hbase Region Server 启动失败
- 阿里云部署mongdb(CentOS)
- shell高级特性-4
- 虹软2.0 离线人脸识别 Android 开发 Demo
- 升级node后还是原来版本问题
- 使用hadoop平台运行Apriori算法
- jQuery+html+css-自己写的分页
- DC-学习
热门文章
- vue打包遇到的坑
- Robot Framework学习笔记
- 定时任务框架Quartz-(一)Quartz入门与Demo搭建
- SP283 NAPTIME - Naptime
- adi i2s 提供的axi_lite接口说明
- 十 LVS 负载均衡
- openwrt查看flash、RAM、CPU信息
- 《剑指offer》翻转单词顺序列
- 敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
- Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required