C# A完成后启动事件B

C# A完成后启动事件B,c#,C#,我用非常简单的函数close();,创建了退出按钮 我该怎么做?声音结束后(2-3秒)app关闭 private void button1_Click(object sender, EventArgs e) { // Play sound this.playSound(); // WAIT FOR END OF SOUND Close(); } private void playSound() { Random random = new Random();

我用非常简单的函数close();,创建了退出按钮

我该怎么做?声音结束后(2-3秒)app关闭

private void button1_Click(object sender, EventArgs e)
{
// Play sound
this.playSound();

// WAIT FOR END OF SOUND

Close();
}

private void playSound()
{
            Random random = new Random();

            // Create list of quit music
            List<System.IO.UnmanagedMemoryStream> sound = new List<System.IO.UnmanagedMemoryStream>
            {
                global::Launcher.Properties.Resources.sound_quit_1,
                global::Launcher.Properties.Resources.sound_quit_2,
                global::Launcher.Properties.Resources.sound_quit_3,
                global::Launcher.Properties.Resources.sound_quit_4,
            };

            // Random, set and play sound
            (new SoundPlayer(sound[random.Next(sound.Count)])).Play();
}
private void按钮1\u单击(对象发送者,事件参数e)
{
//播放声音
这个。playSound();
//等待声音结束
Close();
}
私人音乐播放器()
{
随机=新随机();
//创建退出音乐列表
列表声音=新列表
{
全局::Launcher.Properties.Resources.sound\u退出\u 1,
全局::Launcher.Properties.Resources.sound\u quit\u 2,
全局::Launcher.Properties.Resources.sound\u quit\u 3,
全局::Launcher.Properties.Resources.sound\u quit\u 4,
};
//随机、设置和播放声音
(新的SoundPlayer(声音[随机.下一个(声音.计数)]).Play();
}
如果
playSound()
是同步的,您可以试试

private void button1_Click(object sender, EventArgs e)
{
  // Play sound
  this.playSound();
  BackgroundWorker wk = new BackGroundWorker();
  wk.RunWorkerCompleted += (s,e) => {Thread.Sleep(2000); Close(); };
  wk.RunWorkerAsync();
}
这可以防止GUI看起来被锁定,因为它可以使用更简单的方法

private void button1_Click(object sender, EventArgs e)
{
  // Play sound
  this.playSound();
  Thread.Sleep(2000);
  Close()
}
这将异步播放声音,因此它发生在单独的线程上。缺点是没有关于声音何时结束的信息


相反,您可以在单独的线程上手动使用,并对主线程进行回调,然后关闭应用程序。

应用程序将关闭,因为您正在播放的声音是在与主用户界面线程不同的线程中播放的。您可以随时更改
(新的SoundPlayer(声音[random.Next(sound.Count)]).Play()
(新的SoundPlayer(声音[random.Next(sound.Count)]).PlaySync()如果要使用用户界面(UI)线程播放声音。这样应用程序将等待
声音播放器
停止播放Wave Sound文件,然后关闭
窗体

示例

private void button1_Click(object sender, EventArgs e)
{
    // Play sound
    this.playSound();

    // WAIT FOR END OF SOUND

    Close();
}
private void playSound()
{
    Random random = new Random();

    // Create list of quit music
    List<System.IO.UnmanagedMemoryStream> sound = new List<System.IO.UnmanagedMemoryStream>
    {
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_1,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_2,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_3,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_4,
    };

    // Random, set and play sound
    (new SoundPlayer(sound[random.Next(sound.Count)])).PlaySync(); //We've changed Play(); to PlaySync(); so that the Wave Sound file would be played in the main user interface thread
}
private void按钮1\u单击(对象发送者,事件参数e)
{
//播放声音
这个。playSound();
//等待声音结束
Close();
}
私人音乐播放器()
{
随机=新随机();
//创建退出音乐列表
列表声音=新列表
{
全局::StrowledgeCrusaderLauncher.Properties.Resources.sound\u quit\u 1,
全局::StrowledgeCrusaderLauncher.Properties.Resources.sound\u quit\u 2,
全局::StrowledgeCrusaderLauncher.Properties.Resources.sound\u quit\u 3,
全局::StrowledgeCrusaderLauncher.Properties.Resources.sound\u quit\u 4,
};
//随机、设置和播放声音
(新的SoundPlayer(sound[random.Next(sound.Count)]).PlaySync();//我们已将Play()更改为PlaySync();以便在主用户界面线程中播放Wave sound文件
}
谢谢,

我希望这对您有所帮助:)

不要在GUI事件处理程序中等待。什么是playSound()以及它公开了哪些事件/方法?playSound()是播放声音的函数。我认为这很简单!取决于playSound的功能。同步方法将阻塞,直到完成。如果playSound是异步的,则会有一些系统通知调用已完成。研究C#异步模式。
private void button1_Click(object sender, EventArgs e)
{
    // Play sound
    this.playSound();

    // WAIT FOR END OF SOUND

    Close();
}
private void playSound()
{
    Random random = new Random();

    // Create list of quit music
    List<System.IO.UnmanagedMemoryStream> sound = new List<System.IO.UnmanagedMemoryStream>
    {
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_1,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_2,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_3,
        global::StrongholdCrusaderLauncher.Properties.Resources.sound_quit_4,
    };

    // Random, set and play sound
    (new SoundPlayer(sound[random.Next(sound.Count)])).PlaySync(); //We've changed Play(); to PlaySync(); so that the Wave Sound file would be played in the main user interface thread
}