C#利用子線程刷新主線程分享教程
更新時間:2012年11月22日 11:17:33 作者:
本文將詳細介紹C#利用子線程如何刷新主線程,需要了解更多的朋友可以參考下
要求:如下圖,使用線程操作
1、實時顯示當前時間
2、輸入加數(shù)和被加數(shù),自動出現(xiàn)結(jié)果
分析:兩個問題解決的方式一致,使用子線程進行時間操作和加法操作,然后刷新主線程的控件顯示結(jié)果
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);
}
}
// 定義顯示時間操作委托,用于回調(diào)顯示時間方法
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);
}
}
// 定義加法操作委托,用于回調(diào)加法操作方法
delegate void ShowResultCallBack(string result);
/// <summary>
/// 顯示結(jié)果
/// </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、輸入加數(shù)和被加數(shù),自動出現(xiàn)結(jié)果

分析:兩個問題解決的方式一致,使用子線程進行時間操作和加法操作,然后刷新主線程的控件顯示結(jié)果
復(fù)制代碼 代碼如下:
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);
}
}
// 定義顯示時間操作委托,用于回調(diào)顯示時間方法
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);
}
}
// 定義加法操作委托,用于回調(diào)加法操作方法
delegate void ShowResultCallBack(string result);
/// <summary>
/// 顯示結(jié)果
/// </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
}
}
是不是很簡單呢?
相關(guān)文章
c# Rank屬性與GetUpperBound方法的深入分析
本篇文章是對c#中的Rank屬性與GetUpperBound方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06C#實現(xiàn)數(shù)據(jù)去重的方式總結(jié)
這篇文章主要來和大家一起來討論一下關(guān)于C#數(shù)據(jù)去重的常見的幾種方式,每種方法都有其特點和適用場景,感興趣的小伙伴可以了解一下2023-07-07C#實體對象序列化成Json并讓字段的首字母小寫的兩種解決方法
這篇文章主要介紹了C#實體對象序列化成Json并讓字段的首字母小寫的兩種方法,在這兩種方法中小編比較推薦使用第二種方法,需要的朋友可以參考下2018-06-06unity實現(xiàn)貼圖矩陣運算(旋轉(zhuǎn)平移縮放)
這篇文章主要為大家詳細介紹了unity實現(xiàn)貼圖矩陣運算,旋轉(zhuǎn)平移縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07