尝试运行OpenFileDialog代码时,C#WinForms应用程序冻结。 [英] C# WinForms App freezes when trying to run OpenFileDialog code.
问题描述
我正在编写一个C#WinForms应用程序(VS 2017 Pro v15.7.4),当我尝试运行以下代码时应用程序冻结:
I'm writing a C# WinForms app (VS 2017 Pro v15.7.4) and the app freezes when I try to run the code below:
private void btnAddVideo_Click(object sender, EventArgs e)
{
MessageBox.Show("Congratulations! You just clicked the Add Video button!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Add Video...";
ofd.Filter = "Video (*.avi, *.mpg, *.mpeg, *.wmv)|*.avi;*.mpg;*.mpeg;*.wmv";
if (DialogResult.OK == ofd.ShowDialog())
{
try
{
glbvidVideo = Video.FromFile(ofd.FileName);
clsMessage.bytVideo = File.ReadAllBytes(ofd.FileName);
if (glbvidVideo.Size.Height > glbvidVideo.Size.Width)
pbVideo.Size = pbVideo.MaximumSize = glbszPortraitSize;
else if (glbvidVideo.Size.Width > glbvidVideo.Size.Height)
pbVideo.Size = pbVideo.MaximumSize = glbszLandscapeSize;
glbvidVideo.Owner = pbVideo;
btnRemoveVideo.Enabled = btnPlayVideo.Enabled = btnStopVideo.Enabled =
clsMessage.blMediaPresent = true;
clsMessage.strMediaType = "Video";
}
catch (Exception exVideoLoadError)
{
MessageBox.Show(exVideoLoadError.ToString(), "oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ofd.Dispose();
}
过去我曾多次编写此代码,但它总能顺利运行。 btnAddVideo单击事件处理程序已设置为在单击按钮时运行,我应该看到第一行代码生成的MessageBox,但我没有。相反,
整个应用程序只是冻结,我必须通过按Visual Studio中的"停止"按钮来关闭它。我不知道如何解决它。解决方案或Visual Studio中是否有需要修复的内容?
I've written this code many times in the past and it always ran without a hitch. The btnAddVideo click event handler has been set to run when the button is clicked and I should see the MessageBox generated by the first line of code, but I don't. Instead, the
entire app just freezes and I have to close it by pressing the Stop button in Visual Studio. I'm at a loss as to how to fix it. Could there be something in the solution or Visual Studio that needs fixing?
推荐答案
这是在Win 10上发生的吗?
Is this happening on Win 10?
这篇关于尝试运行OpenFileDialog代码时,C#WinForms应用程序冻结。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!