0

So, I'm trying to do a simple GUI to make music with Beeps on C#. I've been trying but I'm not sure if it's even possible to make a Console.Beep play as I hold down a button for example.

There's the normal Beep() method that just plays a short beep with a medium frequency and there's an overload Beep(int frequency, int duration). What I want to do is actually play it the whole time I'm holding the button, but obviously I can't previously state the duration.

I'm thinking this isn't possible but maybe there is a way?

This is also my first question on the site, so, hey.

4

1 回答 1

3

You could do it like this, i just tested it and it works, and does not lock up the form while running.

    private void Window_MouseDown_1(object sender, MouseButtonEventArgs e)
    {
        // Starts beep on background thread
        Thread beepThread = new Thread(new ThreadStart(PlayBeep));
        beepThread.IsBackground = true;
        beepThread.Start();
    }
    private void PlayBeep()
    {
        // Play 1000 Hz for max amount of time possible
        // So as long as you dont hold the mouse down for 2,147,483,647 milliseconds it should work.
        Console.Beep(1000, int.MaxValue);
    }

    private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
    {
        //Aborts the beep with a new 1ms beep on mouse up which finishes the task.
        Console.Beep(1000, 1);
    }
于 2013-03-30T18:37:09.020 回答