C# 当计时器超时时执行操作
我想发出一声嘟嘟声,表示计时器已过。我将把代码放在哪里,以便在计时器过期时发生C# 当计时器超时时执行操作,c#,C#,我想发出一声嘟嘟声,表示计时器已过。我将把代码放在哪里,以便在计时器过期时发生 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { Timer aTimer = new Timer(); aTim
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Timer aTimer = new Timer();
aTimer.Interval = 1000;
// Hook up the Elapsed event for the timer.
aTimer.Enabled = true;
aTimer.tick += OnTimedEvent;
}
private void OnTimedEvent(object sender, EventArgs e)
{
MessageBox.Show("process");
}
不要忘记添加使用系统。媒体 您是否在询问如何创建系统蜂鸣音?如何显示消息框?如何使用计时器?请具体说明您尝试了什么,以及它是如何工作的。您只是问了有关该消息框的问题,并在评论中表示您得到了它。那个问题和这个问题之间发生了什么?我们不会为你写的。RTFM或谷歌的一些例子,这是涵盖广泛。如果您对所编写的代码有特定的问题,这将是一个更好的问题。我看到您的编辑。您需要将timer1_Tick中的所有代码移动到构造函数中,并使aTimer成为实例变量。谷歌就是一个例子,这是非常基本的东西。@user3478065你得到答案了吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace delete
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 1000;
}
private void timer1_Tick(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer();
sp.Play();
}
}
}