gpt4 book ai didi

c# 每分钟循环一次 - 将代码放在哪里?

转载 作者:行者123 更新时间:2023-11-30 18:59:17 24 4
gpt4 key购买 nike

目前,我正在从 Java 转向 C#,并且充满了疯狂的问题。我正在 Windows 窗体应用程序上尝试新事物,现在,我想创建一个循环,每 1 分钟执行一次代码,问题是我不知道将这段代码放在哪里。例如,表单结构是这样的:

using System;

namespace Tray_Icon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.ShowBalloonTip(5000);


}

private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
{
label1.Text = "Baloon clicked!";
}

private void notifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
label1.Text = "baloon closed!";
}

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{

}

private void option1ToolStripMenuItem_Click(object sender, EventArgs e)
{
//some code here
}

private void option2ToolStripMenuItem_Click(object sender, EventArgs e)
{
//some code here
}

private void option3ToolStripMenuItem_Click(object sender, EventArgs e)
{
label1.Text = "Option 3 clicked!";
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
option1ToolStripMenuItem_Click(this, null);
}

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void btnWrite_Click(object sender, EventArgs e)
{
//code here

}
}
}

我应该把循环代码放在哪里? :(

提前感谢您的重播!

最佳答案

将计时器添加到您的表单:

enter image description here

将其 Interval 属性设置为 60000(一分钟以毫秒为单位)并将 Enabled 设置为 True:

enter image description here

并将事件处理程序附加到 Timer.Tick 事件,例如通过双击表单设计器中的计时器:

private void timer1_Tick(object sender, EventArgs e)
{
// do something here. It will be executed every 60 seconds
}

关于c# 每分钟循环一次 - 将代码放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11500119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com