用C#发出警报,在一天中按特定的固定时间鸣响
我试图在特定的时间发出警报(例如,从上午9:30到下午4点,每隔五分钟)。所以,我想写一段9:30和9:35响起的代码,然后。但每种方法最终都会出错。在我的代码中,我有一个包含时间的字符串,但我不能在if(…)中使用该字符串或组来发出警报。只要一个变量就可以了,我错在哪里了用C#发出警报,在一天中按特定的固定时间鸣响,c#,C#,我试图在特定的时间发出警报(例如,从上午9:30到下午4点,每隔五分钟)。所以,我想写一段9:30和9:35响起的代码,然后。但每种方法最终都会出错。在我的代码中,我有一个包含时间的字符串,但我不能在if(…)中使用该字符串或组来发出警报。只要一个变量就可以了,我错在哪里了 { public partial class Form1 : Form { System.Timers.Timer timer; public Form1()
{
public partial class Form1 : Form
{
System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime currentTime = DateTime.Now;
string[] DailyTime = { 093000, 093500 };
if (((currentTime.Hour * 10000) + (currentTime.Minute *100) + currentTime.Second) == DailyTime)
{
timer.Stop();
try
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Windows\Media\Time interval alarm\FiveH.wav";
player.Play();
计时器初始化方法:
private async void InitializeTimer()
{
while (!timer.Enabled) //keep looping until timer is initialized
{
//if the minute is a multiple of 5 (:00, :05, ...) start the timer
if (DateTime.Now.Minute % 5 == 0 && DateTime.Now.Second == 0)
{
timer.Start();
TriggerAlarm(); //trigger the alarm initially instead of having to wait 5min
}
else
{
await Task.Delay(100);
}
}
}
您可以将时间和报警路径存储在字典中:
Dictionary<TimeSpan, string> dict = new Dictionary<TimeSpan, string>()
{
{ new TimeSpan(9, 30, 0), @"C:\Windows\Media\Time interval alarm\FiveH.wav" },
{ new TimeSpan(9, 35, 0), @"C:\Windows\Media\Time interval alarm\Whatever.wav" },
{ new TimeSpan(9, 40, 0), @"C:\Windows\Media\Time interval alarm\Whatever1.wav" },
{ new TimeSpan(9, 45, 0), @"C:\Windows\Media\Time interval alarm\Whatever2.wav" },
//...
{ new TimeSpan(16, 0, 0), @"C:\Windows\Media\Time interval alarm\Whatever3.wav" }
};
播放声音的方法
private static void TriggerAlarm()
{
TimeSpan alarmTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0);
if (dict.TryGetValue(alarmTime, out string alarmFile))
{
using (SoundPlayer player = new SoundPlayer(alarmFile))
{
player.Play();
}
}
else
{
//this alarm time is not found in the dictionary,
//therefore, no alarm should be played at this time (e.g. 16:05:00)
}
}
您得到了什么错误?错误CS0029无法将类型“int”隐式转换为“string”PM-错误CS0029无法将类型“int”隐式转换为“string”PM-错误CS0019运算符“==”不能应用于类型“int”和
string[]DailyTime={093000,093500}的操作数代码>我怀疑这个编译。稍后,您将尝试将此字符串数组等于整数。。。不可能。错误完全是意料之中的。非常感谢您抽出时间,但这一次最终不会奏效,因为例如;第三次,它应该播放另一个文件。如果它是一个在这些时间的代码,它的工作效果最好。。。。播放这个,然后在这些时间播放那个。例如,在9:45我希望程序播放一个显示为15而不是5的文件,然后在9:50播放上一个显示为5的文件
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
TriggerAlarm();
}
private static void TriggerAlarm()
{
TimeSpan alarmTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0);
if (dict.TryGetValue(alarmTime, out string alarmFile))
{
using (SoundPlayer player = new SoundPlayer(alarmFile))
{
player.Play();
}
}
else
{
//this alarm time is not found in the dictionary,
//therefore, no alarm should be played at this time (e.g. 16:05:00)
}
}