我有一点代码是为了显示一段时间的窗体并播放声音。 但是表格保持打开状态。C#:Thread.Sleep不工作
static void Main(string[] args)
{
SoundPlayer sp = new SoundPlayer();
ShowImage(@"Resources\Fish.png", "Fish", 256, 256, 1000);
sp.SoundLocation = @"Resources\fish.wav";
sp.Play();
}
public static void ShowImage(string img, string title, int width, int height, int timeout)
{
ImageContainer ic = new ImageContainer();
ic.imgView.Image = Image.FromFile(img);
ic.Text = title;
ic.Size = ic.imgView.Image.Size;
ic.Height = height;
ic.Width = width;
ic.ShowDialog();
Thread.Sleep(timeout);
ic.Hide();
ic.Opacity = 0;
ic.Dispose();
}
它只是形式保持开放不会关闭或隐藏。 ImageContainer是一个带有名为imgView的PictureBox的窗体。 我需要它在关闭之前超时1秒。
ShowDialog的()调用正等着你关闭该窗口。您需要一个Timer事件处理程序调用Close()方法的Timer。这需要调用Application.Run()。这种代码会遇到麻烦,你需要阅读一本书。 –