C言語如何利用子線程刷新主線程
你知道C言語如何利用子線程刷新主線程嗎?使用子線程進行時間操作和加法操作,然後刷新主線程的控件顯示結果。下面是小編為大家帶來的關於C言語如何利用子線程刷新主線程的知識,歡迎閲讀。
C言語如何利用子線程刷新主線程使用線程操作
1、實時顯示當前時間
2、輸入加數和被加數,自動出現結果
分析:兩個問題解決的.方式一致,使用子線程進行時間操作和加法操作,然後刷新主線程的控件顯示結果
using System;
using ading;
using s;
namespace WinThread
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
///
/// 初始化
///
///
///
private void frmMain_Load(object sender, EventArgs e)
{
// 控件初始化
= "0";
= "0";
// 顯示時間操作
Thread showTimeThread = new Thread(new ThreadStart(GetTime));
ckground = true;
t();
// 加法操作
Thread addThread = new Thread(new ThreadStart(Add));
ckground = true;
t();
}
#region 顯示時間操作
///
/// 取得實時時間
///
private void GetTime()
{
try
{
while (true)
{
string currentTime = at("{0}.{1}", ngTimeString(),
isecond);
ShowTime(currentTime);
ents();
}
}
catch (Exception ex)
{
eLine(age);
}
}
// 定義顯示時間操作委託,用於回調顯示時間方法
delegate void ShowTimeCallBack(string str);
///
/// 實時顯示時間
///
///
private void ShowTime(string str)
{
if (keRequired)
{
ShowTimeCallBack showTimeCallBack = new ShowTimeCallBack(ShowTime);
ke(showTimeCallBack, new object[] { str });
}
else
{
= str;
}
}
#endregion
#region 加法操作
///
/// 加法操作
///
private void Add()
{
try
{
while (true)
{
int i = t32(());
int j = t32(());
ShowResult((i + j)ring());
ents();
}
}
catch (Exception ex)
{
eLine(age);
}
}
// 定義加法操作委託,用於回調加法操作方法
delegate void ShowResultCallBack(string result);
///
/// 顯示結果
///
///
private void ShowResult(string result)
{
if (keRequired)
{
// 寫法1
//ShowResultCallBack showResultCallBack = new ShowResultCallBack(ShowResult);
//ke(showResultCallBack, new object[] { result });
// 寫法2
//使用委託來賦值
ke(
//委託方法
new ShowResultCallBack(ShowResult),
new object[] { result });
}
else
{
= result;
}
}
#endregion
}
}