如何执行非阻塞的System.Beep()? [英] How can I execute a non-blocking System.Beep()?

查看:669
本文介绍了如何执行非阻塞的System.Beep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我可以执行Console.Beep()。但是,如果你指定一个持续时间为1000或1秒,它将不会执行下一行代码,直到第二个代码。



有任何方法可能

解决方案

您可以在单独的线程中运行它。

  new Thread(()=> Console.Beep )。开始(); 

我今天早上醒来发现对这个答案的评论。



上面的代码也可以通过使用下面的代码在Thread Pool上运行线程来实现。

 操作beep = Console.Beep; 
beep.BeginInvoke((a)=> {beep.EndInvoke(a);},null);

在上面的代码中,重要的是在你的代理上调用EndInvoke,如果你使用BeginInvoke,否则你会遇到内存泄漏。



从MSDN:重要:始终调用EndInvoke完成异步调用。
http://msdn.microsoft.com /en-us/library/2e08f6yc(VS.80).aspx



或者,您可以使用专用的Beep线程在后台运行蜂鸣声当按需不需要每次创建新线程或使用线程池(请参阅Simon Chadwick的注释)。作为一个简单的例子,你可以有以下。注意,我通过1作为maxStackSize,这将确保为此线程提交最小(不是1,最小)堆栈空间。有关此详细信息,请参阅MSDN。

  class BackgroundBeep 
{
static Thread _beepThread;
static AutoResetEvent _signalBeep;

static BackgroundBeep()
{
_signalBeep = new AutoResetEvent(false);
_beepThread = new Thread(()=>
{
for(;;)
{
_signalBeep.WaitOne();
Console.Beep ();
}
},1);
_beepThread.IsBackground = true;
_beepThread.Start();
}

public static void Beep()
{
_signalBeep.Set();
}
}

后面的哔哔声在任何时候与创建新线程是进行以下调用

  BackgroundBeep.Beep(); 


In C# I can perform a Console.Beep(). However, if you specify a duration of say 1000, or 1 second, it will not execute the next line of code until that second passes.

Is there any way possible to execute Console.Beep() in a non-blocking fashion so it will continue to beep and still continue executing the code below it while beeping?

解决方案

You can run it in a separate thread.

new Thread(() => Console.Beep()).Start();

I woke this morning to find flurry of comments on this answer. So I thought I would chime in with some other ideas.

The above can also be achieved running the thread on the Thread Pool, by using the following.

Action beep = Console.Beep;
beep.BeginInvoke((a) => { beep.EndInvoke(a); }, null);

The important thing in the above code is to call EndInvoke on your delegate if you use BeginInvoke otherwise you will experience memory leaks.

From MSDN:Important: Always call EndInvoke to complete your asynchronous call. http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.80).aspx

Alternatively, you can use the dedicated Beep thread to have beeps run in the background when on demand without creating a new thread everytime or using the thread pool (see Simon Chadwick's comment). As a simple example, you could have the following. Notice that I pass 1 as the maxStackSize, this will ensure that the minimum (not 1, minimum) stack space is committed for this thread, see MSDN for more detail on this.

  class BackgroundBeep
  {
    static Thread _beepThread;
    static AutoResetEvent _signalBeep;

    static BackgroundBeep()
    {
      _signalBeep = new AutoResetEvent(false);
      _beepThread = new Thread(() =>
          {
            for (; ; )
            {
              _signalBeep.WaitOne();
              Console.Beep();
            }
          }, 1);
      _beepThread.IsBackground = true;
      _beepThread.Start();      
    }

    public static void Beep()
    {
      _signalBeep.Set();
    }
  }

With this, all you need to do to run a backround beep at anytime with out creating new threads is make the following call

BackgroundBeep.Beep();

这篇关于如何执行非阻塞的System.Beep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆