2014-12-29 74 views
0

基於時凍結位音樂播放按下一個按鈕太快

按NEXT或PREV按鈕太快時

,應用程序將凍結一點(不可控)。


驗證碼:

private void next_event() 
{ 
    _paused = false; 
    if (list[current_index].Rows.Count != 0 && _playing == true) 
    { 
     if (option.random.Checked == true) 
     { 
      nextRandom(1); 
     } 
     else if (option.order.Checked == true) 
     { 
      if (list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) == list[current_index].Rows.Count - 1) 
      { 
       setNowPlaying((Guid)list[current_index].Rows[0].Cells["Key"].Value); 
      } 
      else 
      { 
       setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) + 1].Cells["Key"].Value); 
      } 
     } 
     seek_bar.Value = 0; 
     play(); 
    } 
} 
private void prev_event() 
{ 
    _paused = false; 
    if (list[current_index].Rows.Count != 0 && _playing == true) 
    { 
     if (option.random.Checked == true) 
     { 
      nextRandom(2); 
     } 
     else if (option.order.Checked == true) 
     { 
      if (list[current_index].CurrentRow.Index == 0) 
      { 
       setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.Count - 1].Cells["Key"].Value); 
      } 
      else 
      { 
       setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) - 1].Cells["Key"].Value); 
      } 


     } 
     seek_bar.Value = 0; 
     play(); 
    } 
} 
private void play() // play 
{ 
    button3.Text = "Pause"; 
    dmp_status.Text = "Playing..."; 
    _paused = false; 
    if (list[current_index].Rows.Count != 0) 
    { 
     if (File.Exists(_musicData[_NowPlaying].Cells["FileLocation"].Value.ToString())) 
     { 
      if (_playing == true) wmp.controls.stop(); 
      if(wmp != null) wmp.close(); 
      wmp = new WMPLib.WindowsMediaPlayer(); 
      wmp.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(PlayStateChange); 
      seek_bar.Value = 0; 
      setNowPlayingLabel(_musicData[_NowPlaying]); 
      if (!_musicData.ContainsKey(_LastPlaying)) _LastPlaying = Guid.Empty; 
      setNowPlayingColor(_musicData[_LastPlaying],_musicData[_NowPlaying]); 
      //wmp.URL = list[current_index].CurrentRow.Cells["FileLocation"].Value.ToString(); 
      wmp.URL = _musicData[_NowPlaying].Cells["FileLocation"].Value.ToString(); 
      wmp.controls.play(); 
      wmp.settings.rate = wmp_rate; 
      wmp.settings.volume = volume_pos; 
      if (_playing == false) _playing = true; 
     } 
     else 
     { 
      next_event(); 
     } 
    } 
} 

問題:

我有很難搞清楚他們爲什麼需要播放的每首音樂之前的延遲(凍結),這使得問題時用戶連續按下一個按鈕。

回答

0

我認爲這是由於再入。後續事件在第一個事件尚未完成時觸發時發生。爲了防止這種情況:

// add this line to your class member 
private Object syncRoot = new Object(); 

// add this block to your next_event() method 
if (!Monitor.TryEnter(syncRoot)) return; 

try { 
    // add your existing code here 
} 
finally { 
    Monitor.Exit(syncRoot); 
} 
+0

感謝您的幫助,但是有什麼辦法可以取消第一個過程並立即進入下一個過程? –

相關問題