C# 当按下“下一步”按钮太快时,音乐播放器有点冻结

C# 当按下“下一步”按钮太快时,音乐播放器有点冻结,c#,.net,wmplib,C#,.net,Wmplib,基于: C窗口窗体&使用WMPLib 问题是: 当按下“下一步”或“上一步”按钮太快时,应用程序将冻结,有点无法控制 守则: private void next_event() { _paused = false; if (list[current_index].Rows.Count != 0 && _playing == true) { if (option.random.Checked == true) {

基于:

C窗口窗体&使用WMPLib

问题是:

当按下“下一步”或“上一步”按钮太快时,应用程序将冻结,有点无法控制

守则:

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(); 
        }
    }
}
问题是:


我很难理解为什么他们在播放每首音乐之前需要延迟冻结,这使得用户连续按“下一步”按钮时出现问题。

我认为这是由于重新播放。在第一个事件尚未完成时触发后续事件时发生。为防止这种情况发生:

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

谢谢您的帮助,但是有没有办法取消第一个过程并立即转到下一个过程?