0

基于:

C# windows 窗体和使用 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(); 
        }
    }
}

问题:

我很难弄清楚为什么他们在播放每首音乐之前需要延迟(冻结),这在用户连续按下下一个按钮时会产生问题。

4

1 回答 1

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);
}
于 2014-12-29T13:43:10.573 回答