简体   繁体   English

VS2010 C#函数使用循环

[英]VS2010 C# function using a loop

I am currently implementing my final year project as a Music Software Development student. 我目前正在作为音乐软件开发学生实施我的最后一年项目。 I am making a software that plays a midi track that is generated earlier in the process. 我正在制作一个软件,播放在此过程中早期生成的midi音轨。

In my interface, there is a button "play" and a button "stop". 在我的界面中,有一个按钮“play”和一个“stop”按钮。 When I click "play", a loop is called that plays this track. 当我点击“播放”时,会调用一个播放此曲目的循环。 In this loop, I call a function from an instance of an other class. 在这个循环中,我从另一个类的实例调用一个函数。

What I would like is that when the user press "pause", the loop is temporary stopped, and when he press "pause" again, it goes back exactly where it was in the loop. 我想要的是当用户按下“暂停”时,循环暂时停止,当他再次按下“暂停”时,它会准确地返回循环中的位置。

So far, when I press play, the track plays exactly how I want to, but I can't use any other element of my window until the loop is not completly processed. 到目前为止,当我按下播放时,轨道按照我想要的方式播放,但是在循环未完全处理之前我不能使用窗口的任何其他元素。

Here is my code. 这是我的代码。

private void playStopButton_Click(object sender, RoutedEventArgs e)
    {
        // Initialising my output midi devices
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.bassChannel, information.bassSound));//bass
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.guitarChannel, information.guitarSound));//guitar

        for (int barNum = 0; barNum < Globals.numberOfBars; barNum++)
        {
            // playing the first beat
            rythmicPattern.play(0, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //second beat
            rythmicPattern.play(1, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //thrid beat
            rythmicPattern.play(2, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //fourth beat
            rythmicPattern.play(3, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);
        }
    }

private void playPauseButton_Click(object sender, RoutedEventArgs e)
        {
            // test if the track is being played
            if (isPlaying)
                rythmicPattern.Pause();
            else
                // if it was already paused, playing back from where I stopped
                rythmicPattern.PlayAfterPause(beatNumber);
        }

Thank you for your help. 谢谢您的帮助。 Greg 格雷格

您必须使用线程,您可以在另一个线程中播放声音,因此您可以执行暂停方法检查此链接.NET中的多线程:简介和建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM