2014-12-29 66 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

感谢您的帮助,但是有什么办法可以取消第一个过程并立即进入下一个过程? –

相关问题