我的程序似乎崩溃了,但它仍然... 运行

My program seemed to crashed but it's still... running

我正在编写一个音乐播放器以供评估。目前我需要制作它以便曲目按顺序播放。

目前它播放音乐的位是这样的:

repeat
//Other code that's related to responding to user input
while i < trackCount - 1 do
    begin
        if not MusicPlaying() then
        begin
            PlayMusic(trackName, 1);
            trackNumber := trackNumber + 1;
            i := i + 1;
        end;
    end;
until WindowCloseRequested();

基本上是说"While the list is not finished yet, if no music is playing, then play a track and increase i so the next track can be played once it's finished"。显然,这使程序崩溃了,或者看起来是这样。点击播放后我无法与程序交互,但音乐仍在按顺序播放,这告诉我逻辑运行良好。之后程序 return 恢复正常,但当我再次尝试播放列表时 return 又回到了相同的状态。这是否意味着它是一个错误的代码,我应该试着想出一个不同的方法来解决它?或者有什么我不知道的关于循环内循环的事情吗?

不,您的程序并没有真正崩溃,它只是在 while i < trackCount - 1 do 循环中停滞了。

i 仅在 MusicPlaying() return 为 false 时更改(递增),这大概只发生在最初和曲目播放完毕之后。在这期间,您的程序陷入 while 循环,只是反复等待 MusicPlaying() 到 return false,因此看起来没有响应。

我怀疑 repeat..until 循环是您程序的主循环。如果是这样,那么您只需将单词 while 更改为 if

repeat
    // Other code that's related to responding to user input
    if i < trackCount - 1 do
    begin
        if not MusicPlaying() then
        begin
            PlayMusic(trackName, 1);
            trackNumber := trackNumber + 1;
            i := i + 1;
        end;
    end;
until WindowCloseRequested();