2012-08-31 103 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,因爲它非常易於使用,並且可以幫助您實現他所建議/您正在尋找的內容。