新手求教:一个方法要执行5次,每次间隔2秒,该怎么做定时器
c#吧
全部回复
仅看楼主
level 2
lizhongrui 楼主
RT,谢谢!
2013年05月19日 14点05分 1
level 7
timer的属性Interval设置为2000,在timer事件中设置一个int count=1
timer事件的方法体为
{
if(count==5)
timer1.Enabled=false
else
///////
count++
}
这就可以了
2013年05月19日 14点05分 2
level 11
int count=0;while(count<5){dosomething();count++;Sleep(2000);}
2013年05月19日 18点05分 3
我试过这种方式,直接死机啊。。。
2013年05月20日 11点05分
回复 lizhongrui :在新线程中写这个
2013年05月20日 18点05分
level 9
自己写个线程就是了呀
2013年05月19日 18点05分 4
level 11
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 TimerDemo
{
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 2000;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (count<5)
{
count++;
//写你要做的事情
}
else
{
timer1.Stop();
}
}
}
}
2013年05月20日 15点05分 5
level 11
int count = 1;
private void timer1_Tick(object sender, EventArgs e)
{
if (count <= 5)
{
button1.Text = count.ToString();
if (count == 5)
{
timer1.Stop();
}
}
count++;
}
2013年05月20日 15点05分 6
谢谢!
2013年05月20日 18点05分
level 8
2013年05月20日 15点05分 7
1