C#利用子线程刷新主线程分享教程
程序员文章站
2023-11-17 13:14:10
要求:如下图,使用线程操作 1、实时显示当前时间 2、输入加数和被加数,自动出现结果 分析:两个问题解决的方式一致,使用子线程进行时间操作和加法操作,然后刷新主...
要求:如下图,使用线程操作
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
}
}
是不是很简单呢?
下一篇: js代码延迟一定时间后执行一个函数的实例