网上提到这个方法:
[DLLImport("kernel32.dll")]
private static extern int beep(int a ,int b)public static void Main()
{
beep(1000,1000);
}
这样的确可以实现报警:但是我需要实现 当程序报警之后,响过之后如果用户没有关掉,
那么程序自动间隔 30秒在做一次报警并且:这个间隔用户可以设置!

解决方案 »

  1.   

    Timer控件+全局变量,可以实现。
    另外提醒楼主,你引用API的方法有点小问题,会导致CA1060警告。
      

  2.   


       public class RunTime
        {
            private Timer _timer = new Timer(); 
            public void GetPic(object sender, ElapsedEventArgs e)
            {
    Main();
                 
    }        public void Run()
            {
                _timer.Interval = 30*1000;
                _timer.Elapsed += new ElapsedEventHandler(GetPic);
                _timer.Start();
            }
    public void stop()
    {
    _timer.Stop();
    }[DLLImport("kernel32.dll")]
    private static extern int beep(int a ,int b)public static void Main()
    {
    beep(1000,1000);
    }    }
    大概的代码
    在你的from打开的时候Run(),在关闭的时候stop()
      

  3.   

    [DllImport("kernel32.dll")]
    private static extern int beep(int a, int b);public static void Main()
    {
        //打印个有好信息
        Console.WriteLine("Keyboard error , Press F1 to continue...");
        //定义退出信号
        bool running = true;
        //开线程去提醒
        new Thread((ThreadStart)delegate
            {
                while (running)
                {
                    beep(1000, 1000);
                    Thread.Sleep(30 * 1000);
                }
            }).Start();
        while (Console.ReadKey().Key != ConsoleKey.F1) //一直等到按了F1才离开while循环
        {
            //打印个有好信息
            Console.WriteLine("Keyboard error , Press F1 to continue...");
        }
        //停止信号,进而停止线程
        running = false;
    }
      

  4.   

    [DllImport("kernel32.dll", EntryPoint = "Beep")]
    public static extern int Beep(
        int dwFreq,
        int dwDuration
    );public static void Main()
    {
        //打印个有好信息
        Console.WriteLine("Keyboard error , Press F1 to continue...");
        //定义退出信号
        bool running = true;
        //开线程去提醒
        new Thread((ThreadStart)delegate
        {
            while (running)
            {
                Beep(1000, 1000);
                int tick = Environment.TickCount;
                //优化sleep,加快响应
                while (Environment.TickCount - tick < 30000 && running) Thread.Sleep(10);
            }
        }).Start();
        while (Console.ReadKey().Key != ConsoleKey.F1) //一直等到按了F1才离开while循环
        {
            //打印个有好信息
            Console.WriteLine("Keyboard error , Press F1 to continue...");
        }
        //停止信号,进而停止线程
        running = false;
    }
      

  5.   


    请问如何让启用的线程的//打印个有好信息
        Console.WriteLine("Keyboard error , Press F1 to continue...");
    ,没有那个黑色DOS界面呢