2012-08-31 56 views
4

我有一个文字说明音乐开/关的按钮。打开/关闭音乐按钮Console.Beep

用下面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); 
} 

我的问题是我怎么告诉按钮关掉音乐,如果它再次单击,然后把音乐重新如果再等等点击等等。

+3

将所有值存储在数组中,用for循环重写代码,并在循环中检查是否设置了标志。然而,这意味着你的代码在非ui线程中运行。 –

+1

(它是超级马里奥兄弟主题曲...八九不离十) –

+9

耶......只是做我所有的同事看着我:d –

回答

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方法播放,停止方法停止。理想情况下,您必须在完成歌曲后处理互斥锁。

0

@Steve B在评论中有什么是正确的方法。您可能需要查看Timer Class,因为它非常易于使用,并且可以帮助您实现他所建议/您正在寻找的内容。