英文:
How to use timer to click buttons in C#
问题
如何使用定时器每3秒触发点击按钮事件?
我正在尝试通过定时器自动触发旋转按钮来旋转两个图片框中的2张图片,但似乎不起作用。我以前从未使用过定时器,所以这是我第一次尝试。有人知道我的代码有什么问题,或者有其他代码建议吗?谢谢
我正在使用的代码:
private void timer1_Tick(object sender, EventArgs e)
{
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private void timerStartButton_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timer1.Stop();
}
英文:
How to use timers to trigger click button event every 3 seconds?
I'm trying to rotate 2 pictures in pictureboxes by triggering the rotate button automaticly using timer but it seems doesnt works. I never used timer before so this is my first time. Anyone know whats wrong with my code or any other code suggestion for it? Thanks
Code I'm using
private void timer1_Tick(object sender, EventArgs e)
{
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private void timerStartButton_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timer1.Stop();
}
答案1
得分: 1
以下是您提供的代码的翻译部分:
public partial class Form1 : Form
{
// 用于跟踪计时器是否正在运行的变量。
private bool _timerRunning;
public Form1()
{
InitializeComponent();
}
private async Task StartTimer()
{
// 将其设置为true
_timerRunning = true;
while (_timerRunning)
{
// 调用rotateRightButton_Click(您需要的操作)
rotateRightButton_Click(this, EventArgs.Empty);
pictureBox1.Refresh();
pictureBox2.Refresh();
// 等待3秒(但不要阻塞GUI线程)
await Task.Delay(3000);
}
}
private void rotateRightButton_Click(Form1 form1, EventArgs empty)
{
// 进行您的操作
}
private async void buttonStart_Click(object sender, EventArgs e)
{
// 如果已经启动,不要再次启动它。
if (_timerRunning)
return;
// 启动它。
await StartTimer();
}
private void buttonStop_Click(object sender, EventArgs e)
{
// 停止它。
_timerRunning = false;
}
}
请注意,代码中的注释和变量名保持不变。
英文:
It's even possible (and more simple) with tasks
public partial class Form1 : Form
{
// variable to keep track if the timer is running.
private bool _timerRunning;
public Form1()
{
InitializeComponent();
}
private async Task StartTimer()
{
// set it to true
_timerRunning = true;
while (_timerRunning)
{
// call the rotateRightButton_Click (what you want)
rotateRightButton_Click(this, EventArgs.Empty);
pictureBox1.Refresh();
pictureBox2.Refresh();
// wait for 3 seconds (but don't block the GUI thread)
await Task.Delay(3000);
}
}
private void rotateRightButton_Click(Form1 form1, EventArgs empty)
{
// do your thing
}
private async void buttonStart_Click(object sender, EventArgs e)
{
// if it's already started, don't start it again.
if (_timerRunning)
return;
// start it.
await StartTimer();
}
private void buttonStop_Click(object sender, EventArgs e)
{
// stop it.
_timerRunning = false;
}
}
答案2
得分: 0
timer1.Interval = 3000; // 将间隔设置为3秒,然后调用Time Elapsed事件
timer1.Elapsed += Time_Elapsed;
// 事件
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// 每3秒触发一次
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
英文:
timer1.Interval = 3000; // set interval to 3 seconds and then call Time Elapsed event
timer1.Elapsed += Time_Elapsed;
//Event
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// will be triggered in every 3 seconds
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论