System.Timers.Timer t; int sum = 0; int qian; int bai; int shi; int ge; public 测试定时启动() { InitializeComponent(); } //1.使用System.Timers.Timer //2.thread Thread th;//定义线程 private void 测试定时启动_Load(object sender, EventArgs e) { textBox1.Text = "00.00"; t = new System.Timers.Timer(1000); t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); th = new Thread(calculate);//2.线程方法 th.IsBackground = true; } private void theout(object sender, EventArgs e) { sum++; qian = sum / 1000; bai = sum % 1000 / 100; shi = sum % 100 / 10; ge = sum % 10; textBox1.Text = qian.ToString() + bai.ToString() + "." + shi.ToString() + ge.ToString(); lbmessge.Items.Insert(0, "我1111"); } private void button1_Click(object sender, EventArgs e) { t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; //如果不写下面这句会有一个异常。 //异常:线程间操作无效: 从不是创建控件"richtextbox"的线程访问它 //但这不是最好的方法。如果只有一个进程调用richtextbox而已。就可以用下面这句 //如果有多个线程调用richtextbox等控件。就要用委托。见thread第二种方法 Control.CheckForIllegalCrossThreadCalls = false; } private void calculate()//线程的第一种方法 { while (true) { sum++; qian = sum / 1000; bai = sum % 1000 / 100; shi = sum % 100 / 10; ge = sum % 10; textBox1.Text = qian.ToString() + bai.ToString() + "." + shi.ToString() + ge.ToString(); Thread.Sleep(1000); } }
public partial class 测试定时启动 : Form { System.Timers.Timer t; int sum = 0; int qian; int bai; int shi; int ge; public 测试定时启动() { InitializeComponent(); } //1.使用System.Timers.Timer //2.thread Thread th;//定义线程 private void 测试定时启动_Load(object sender, EventArgs e) { textBox1.Text = "00.00"; th = new Thread(calculate); th.IsBackground = true; } private void calculate()//线程的第一种方法 { while (true) { sum++; qian = sum / 1000; bai = sum % 1000 / 100; shi = sum % 100 / 10; ge = sum % 10; textBox1.Text = qian.ToString() + bai.ToString() + "." + shi.ToString() + ge.ToString(); Thread.Sleep(1000); } } private void button1_Click(object sender, EventArgs e) { th.Start(); Control.CheckForIllegalCrossThreadCalls = false; // 同2,不写会报错 } }
System.Timers.Timer t; int sum = 0; int qian; int bai; int shi; int ge; public 测试定时启动() { InitializeComponent(); }
//1.使用System.Timers.Timer //2.thread Thread th;//定义线程 private void 测试定时启动_Load(object sender, EventArgs e) { textBox1.Text = "00.00"; t = new System.Timers.Timer(1000); t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); th = new Thread(calculate);//2.线程方法 th.IsBackground = true; } private void theout(object sender, EventArgs e) { sum++; qian = sum / 1000; bai = sum % 1000 / 100; shi = sum % 100 / 10; ge = sum % 10; textBox1.Text = qian.ToString() + bai.ToString() + "." + shi.ToString() + ge.ToString(); lbmessge.Items.Insert(0, "我1111"); } private void button1_Click(object sender, EventArgs e) { t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; //如果不写下面这句会有一个异常。 //异常:线程间操作无效: 从不是创建控件"richtextbox"的线程访问它 //但这不是最好的方法。如果只有一个进程调用richtextbox而已。就可以用下面这句 //如果有多个线程调用richtextbox等控件。就要用委托。见thread第二种方法 Control.CheckForIllegalCrossThreadCalls = false; } private void calculate()//线程的第一种方法 { while (true) { sum++; qian = sum / 1000; bai = sum % 1000 / 100; shi = sum % 100 / 10; ge = sum % 10; textBox1.Text = qian.ToString() + bai.ToString() + "." + shi.ToString() + ge.ToString(); Thread.Sleep(1000); } }