C#利用子线程刷新主线程分享教程
时间:2023-05-16 13:25:46
要求:如下图,使用线程操作
1、实时显示当前时间
2、输入加数和被加数,自动出现结果
分析:两个问题解决的方式一致,使用子线程进行时间操作和加法操作,然后刷新主线程的控件显示结果
using System;
using System.Threading;
using System.Windows.Forms;
namespace WinThread
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_Load(object sender, EventArgs e)
{
// 控件初始化
this.txtOne.Text = "0";
this.txtSecond.Text = "0";
// 显示时间操作
Thread showTimeThread = new Thread(new ThreadStart(GetTime));
showTimeThread.IsBackground = true;
showTimeThread.Start();
//加法操作
Thread addThread = new Thread(new ThreadStart(Add));
addThread.IsBackground = true;
addThread.Start();
}
#region 显示时间操作
/// <summary>
/// 取得实时时间
/// </summary>
private void GetTime()
{
try
{
while (true)
{
string currentTime = string.Format("{0}.{1}", DateTime.Now.ToLongTimeString(), DateTime.Now.Millisecond);
ShowTime(currentTime);
Application.DoEvents();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// 定义显示时间操作委托,用于回调显示时间方法
delegate void ShowTimeCallBack(string str);
/// <summary>
/// 实时显示时间
/// </summary>
/// <param name="str"></param>
private void ShowTime(string str)
{
if (this.txtCurrentTime.InvokeRequired)
{
ShowTimeCallBack showTimeCallBack = new ShowTimeCallBack(ShowTime);
this.Invoke(showTimeCallBack, new object[] { str });
}
else
{
this.txtCurrentTime.Text = str;
}
}
#endregion
#region 加法操作
/// <summary>
/// 加法操作
/// </summary>
private void Add()
{
try
{
while (true)
{
int i = Convert.ToInt32(this.txtOne.Text.Trim());
int j = Convert.ToInt32(this.txtSecond.Text.Trim());
ShowResult((i + j).ToString());
Application.DoEvents();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// 定义加法操作委托,用于回调加法操作方法
delegate void ShowResultCallBack(string result);
/// <summary>
/// 显示结果
/// </summary>
/// <param name="result"></param>
private void ShowResult(string result)
{
if (this.txtResult.InvokeRequired)
{
// 写法1
//ShowResultCallBack showResultCallBack = new ShowResultCallBack(ShowResult);
//this.Invoke(showResultCallBack, new object[] { result });
// 写法2
//使用委托来赋值
this.txtResult.Invoke(
//委托方法
new ShowResultCallBack(ShowResult),
new object[] { result });
}
else
{
this.txtResult.Text = result;
}
}
#endregion
}
}
是不是很简单呢?
标签:C#,子线程,刷新,主线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java如何连续执行多条cmd命令
2023-07-13 13:10:41
Android实现购物车功能
2022-08-14 19:06:55
![](https://img.aspxhome.com/file/2023/5/109165_0s.png)
Java基于解释器模式实现定义一种简单的语言功能示例
2021-06-18 15:53:42
![](https://img.aspxhome.com/file/2023/2/72382_0s.png)
Android 自定义输入手机号自动添加分隔符
2022-08-15 12:32:27
![](https://img.aspxhome.com/file/2023/4/139254_0s.png)
浅析Java多线程同步synchronized
2023-05-20 15:52:29
Android进度条ProgressBar的实现代码
2023-01-30 07:12:22
![](https://img.aspxhome.com/file/2023/1/138221_0s.jpg)
Java读取并下载网络文件的方法
2023-03-18 11:47:05
C#定时关闭窗体实例
2023-07-09 22:53:25
adb通过wifi连接android设备流程解析
2021-12-27 08:39:37
![](https://img.aspxhome.com/file/2023/6/89526_0s.png)
Java中ResultSetMetaData 元数据的具体使用
2021-06-25 12:38:13
![](https://img.aspxhome.com/file/2023/2/65732_0s.png)
C#窗体实现酒店管理系统
2023-05-23 18:58:47
![](https://img.aspxhome.com/file/2023/5/79125_0s.jpg)
SpringBoot深入了解日志的使用
2023-01-06 15:40:34
![](https://img.aspxhome.com/file/2023/7/65177_0s.png)
C#开发的人脸左右相似度计算软件源码分析
2023-08-26 05:18:41
spring中的注解事务演示和添加步骤详情
2023-03-03 08:32:48
![](https://img.aspxhome.com/file/2023/3/77983_0s.png)
Android Studio多工程引用同一个library项目配置的解决方法
2022-04-07 16:39:40
![](https://img.aspxhome.com/file/2023/2/138972_0s.png)
Android打包上传AAR文件到Maven仓库的示例
2023-07-02 15:14:14
![](https://img.aspxhome.com/file/2023/8/138978_0s.png)
Java 多线程并发ReentrantLock
2022-02-23 11:53:23
![](https://img.aspxhome.com/file/2023/9/66949_0s.png)
android实现QQ微信侧滑删除效果
2021-07-11 15:58:11
![](https://img.aspxhome.com/file/2023/0/97870_0s.jpg)
关于Java集合框架面试题(含答案)上
2021-06-02 13:29:44
C#实现两个richtextbox控件滚动条同步滚动的简单方法
2023-10-14 19:34:51
![](https://img.aspxhome.com/file/2023/5/129625_0s.png)