【发布时间】:2016-04-29 01:55:46
【问题描述】:
我有一个打开 OpenFileDialog 的按钮。当我编译应用程序时,第一次运行它,按下按钮,选择文件,然后按下接受对话框按钮,它会等待大约一分钟,然后将选定的文件添加到我的列表框中。
如果我关闭应用程序,重新启动它并执行与上述相同的操作,一切都会快速正常运行。从那时起,它总是快速运行。这只是我编译后第一次运行它,因为它太慢了。
代码摘录如下。对话框可能有什么问题?为什么第一次运行很慢?谢谢。
void ButtonAddClick(object sender, EventArgs e)
{
this.openFileDialog.FileName = String.Empty;
this.openFileDialog.InitialDirectory = this.openPath;
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in this.openFileDialog.FileNames)
{
if (!File.Exists(file))
{
this.ShowStatus("Error occured selecting file " + Path.GetFileName(file));
}
else if (!this.listBoxFiles.Items.Contains(file))
{
this.listBoxFiles.Items.Insert(0, file);
}
else{
this.ShowStatus("File " + Path.GetFileName(file) + " already selected");
}
}
}
if (this.listBoxFiles.Items.Count > 0)
{
this.openPath = Path.GetDirectoryName(this.listBoxFiles.Items[0].ToString());
this.listBoxFiles.Enabled = true;
this.buttonClear.Enabled = true;
this.buttonFolder.Enabled = true;
}
}
【问题讨论】:
-
你是在VS中运行应用程序,调试吗?您是否尝试过手动编译然后从发布文件夹运行应用程序以查看是否存在相同的问题?
-
我从哪里运行它并不重要,无论它是处于调试模式还是发布模式都无关紧要。我使用 SharpDevelop。我应该只添加 openfilesdialog 第一次很慢,任何其他对话框都可以正常运行。
-
在一分钟延迟期间闯入调试器,看看你的线程在做什么。
-
雷蒙德,我该怎么做?
-
不一定。无论是什么原因,它都可能是在第一次打开时缓存文件列表。或者网络连接正在尝试连接到他们的服务器等。请尝试在另一台计算机上运行您的应用。
标签: c# .net openfiledialog