C#利用子线程刷新主线程分享教程
- 作者: 莪_o0O
- 来源: 51数据库
- 2021-08-16
要求:如下图,使用线程操作
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
}
}
是不是很简单呢?
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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
