C#使控制台程序发出哔哔声

发布时间 2023-07-17 15:23:44作者: 初久的私房菜

个人主要用于,检测电商指定物品,轮询接口,当检测到库存后,程序声音提示,通知手动去下单。

代码如下

using System;
using System.Timers;

static class BeepIO
{
    static void Main(string[] args)
    {
        using (var timer = new Timer(120000))
        {
            timer.Elapsed += OnTimedEvent;
            timer.AutoReset = true;
            timer.Enabled = true;
            Console.WriteLine("Press Enter to stop the beep timer.");
            timer.Start();
            Console.ReadLine();
        }
    }

    private static void OnTimedEvent(object sender, ElapsedEventArgs e)
    {
        // 播放 1060Hz 频率的声音,持续 1060 毫秒
        Console.Beep(1060, 1060);
    }
}