4

音楽のオン/オフというテキストのボタンがあります。

次のConsole.Beep()コードを使用します。

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);
}

私の質問は、もう一度クリックすると音楽をオフにし、もう一度クリックすると音楽をオンに戻すようにボタンに指示する方法です。

4

3 に答える 3

6

私は質問に答えるために完全なコードを書くことに慣れていませんが、今の私がどれだけ退屈しているか、そして演奏されている音楽を考えると、小さなステートマシンを作りたいという衝動と戦うことができませんでした.

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();
        }
    }
}

使用法: Player クラスをインスタンス化します。再生するには Play メソッドを使用し、停止するには Stop メソッドを使用します。理想的には、曲の終了後にミューテックスを破棄する必要があります。

于 2012-08-31T14:40:50.130 に答える
0

このスレッドを参照してください: How do I run a simple bit of code in a new thread?

UI 自体に対して別のスレッドとして音楽を実行する必要があります。

于 2012-08-31T14:25:16.343 に答える
0

コメントにある @Steve B が正しい方法です。作業が非常に簡単で、彼が提案していること/探していることを達成するのに役立つため、Timer Classを確認することをお勧めします。

于 2012-08-31T14:18:42.620 に答える