using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
//假設你要顯示的變數是n
private int n;
//定義一個定時器
private System.Threading.Timer tm;
public Form1()
InitializeComponent();
n = 0;
//初始化定時器,每隔1000毫秒執行一個Exec方法
tm = new System.Threading.Timer(new System.Threading.TimerCallback(Exec), null, 0, 1000);
}
private void Exec(object state)
n++;
//在這裡顯示你的變數
SetLable(n.ToString());
#region 提示資訊方法
private delegate void SetLableCallback(string str);
public void SetLable(string str)
if (IsDisposed)
return;
if (label1.IsDisposed)
// InvokeRequired需要比較呼叫執行緒ID和建立執行緒ID
// 如果它們不相同則返回true
if (label1.InvokeRequired)
SetLableCallback o = new SetLableCallback(SetLable);
Invoke(o, new object[] { str });
else
label1.Text = str;
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//假設你要顯示的變數是n
private int n;
//定義一個定時器
private System.Threading.Timer tm;
public Form1()
{
InitializeComponent();
n = 0;
//初始化定時器,每隔1000毫秒執行一個Exec方法
tm = new System.Threading.Timer(new System.Threading.TimerCallback(Exec), null, 0, 1000);
}
private void Exec(object state)
{
n++;
//在這裡顯示你的變數
SetLable(n.ToString());
}
#region 提示資訊方法
private delegate void SetLableCallback(string str);
public void SetLable(string str)
{
if (IsDisposed)
return;
if (label1.IsDisposed)
return;
// InvokeRequired需要比較呼叫執行緒ID和建立執行緒ID
// 如果它們不相同則返回true
if (label1.InvokeRequired)
{
SetLableCallback o = new SetLableCallback(SetLable);
Invoke(o, new object[] { str });
}
else
{
label1.Text = str;
}
}
#endregion
}
}