Question

I have a button with text that says Music On/Off.

With the following Console.Beep() code:

private void button1_Click(object sender, EventArgs e)
{
    Console.Beep(659, 125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(523, 125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(375);
    Console.Beep(392, 125);
    Thread.Sleep(375);
    Console.Beep(523, 125);
    Thread.Sleep(250);
    Console.Beep(392, 125);
    Thread.Sleep(250);
    Console.Beep(330, 125);
    Thread.Sleep(250);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(466, 125);
    Thread.Sleep(42);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(392, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(880, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(587, 125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(250);
    Console.Beep(392, 125);
    Thread.Sleep(250);
    Console.Beep(330, 125);
    Thread.Sleep(250);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(466, 125);
    Thread.Sleep(42);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(392, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(880, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(587, 125);
    Console.Beep(494, 125);
    Thread.Sleep(375);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(698, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(698, 125);
    Thread.Sleep(625);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(622, 125);
    Thread.Sleep(250);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(523, 125);
    Thread.Sleep(1125);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(698, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(698, 125);
    Thread.Sleep(625);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(622, 125);
    Thread.Sleep(250);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(523, 125);
    Thread.Sleep(625);
}

My question is how do I tell the button to turn off the music if its clicked again, and then turn the music back on if its clicked again and so on and so forth.

Was it helpful?

Solution

I'm not used to write the full code to answer a question, but given how much I'm bored right now, and given the music played, I couldn't fight the urge to make a small state-machine.

public class Player
{
    private Thread Thread { get; set; }

    private ManualResetEventSlim Mutex { get; set; }

    public Player()
    {
        this.Mutex = new ManualResetEventSlim(false);

        this.Thread = new Thread(this.PlayThread);
        this.Thread.Start();
    }

    public void Play()
    {
        this.Mutex.Set();
    }

    public void Stop()
    {
        this.Mutex.Reset();
    }

    private static IEnumerable<Action> PlaySong()
    {
        yield return () => Console.Beep(659, 125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(330, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(466, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(880, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(587, 125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(330, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(466, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(880, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(587, 125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(625);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(1125);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(625);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(625);
    }

    private void PlayThread()
    {
        foreach (var action in PlaySong())
        {
            if (!this.Mutex.IsSet)
            {
                this.Mutex.Wait();
            }

            action();
        }
    }
}

Usage: instantiate the Player class. Use the Play method to play, and the Stop method to stop. Ideally, you would have to dispose the mutex after finishing the song.

OTHER TIPS

What @Steve B has in the comment is the correct method. You might want to look at the Timer Class, as its quite easy to work with, and will help you achieve what he is suggesting / you're looking for.

See this thread: How do I run a simple bit of code in a new thread?

You need to run the music as a separate thread to the UI itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top