利用System.Timers.Timer实现实时更新
public partial class Form1 : Form
{
System. Timers. Timer timer;
public delegate void SetControlValue ( string value ) ;
public Form1 ( )
{
InitializeComponent ( ) ;
}
private void Form1_Load ( object sender, EventArgs e)
{
InitTimer ( ) ;
}
private void btn_start_Click ( object sender, EventArgs e)
{
timer. Start ( ) ;
}
private void InitTimer ( )
{
int interval = 1000 ;
timer = new System. Timers. Timer ( interval) ;
timer. AutoReset = true ;
timer. Enabled = true ;
timer. Elapsed += new System. Timers. ElapsedEventHandler ( MyMethod) ;
}
private void MyMethod ( object sender, System. Timers. ElapsedEventArgs e)
{
string show = "" ;
this . Invoke ( new SetControlValue ( SetTextBoxText) , show) ;
}
private void SetTextBoxText ( string value )
{
txt_result. Text = value ;
}
}