如何在 C# 中使用“打开/保存文件”对话框时防止资源泄漏
我们在桌面应用程序(C#)中使用保存/OPN 文件对话框。 当我们第一次打开对话框时,句柄增加 100。关闭对话框后,句柄不会减少。从下一次开始,句柄增加 10 左右,然后减少 2 到 4 个
。我们尝试通过调用 dispose 并将其设置为 null 来减少句柄。 还尝试使用块。 但他们都没有解决这个问题。
您能告诉我有什么解决办法吗?或者我们可以使用任何自定义控件吗?
请对此提出建议,
提前致谢
代码: 代码是
SaveFileDialog objSaveDialog = new SaveFileDialog();
try
{
objSaveDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
objSaveDialog.Title = "Save to Text File";
//objSaveDialog.ShowDialog();
DialogResult dlgResult = objSaveDialog.ShowDialog();
objSaveDialog.Dispose();
if (dlgResult == DialogResult.OK)
{
string strSaveFilePath = objSaveDialog.FileName;
if (!string.IsNullOrEmpty(strSaveFilePath))
{
TextWriter myTxtWriter = new StreamWriter(strSaveFilePath, false);
for (int index = 0; index < 10000; index++)
{
myTxtWriter.WriteLine("sample text.....................................");
}
myTxtWriter.Flush();
myTxtWriter.Close();
myTxtWriter.Dispose();
}
}
}
finally
{
if (objSaveDialog != null)
{
objSaveDialog = null;
//((IDisposable)objSaveDialog).Dispose();
}
}
we are using save/opn file dialog in our desktop application(C#).
When we open the dialog for the first time, handles are increased by 100. After closing the dialog the handles are not getting reduced. From the next time onwards handles are increasing by 10 or so and getting reduced by 2 to 4.
We tried decreasing the handles by calling dispose and making it null.
And also tried with using block.
But none of them solved the issue.
Could you please tell me any work around for this? Or can we use any custom control or so
Please advice on this
Thanks in advance
code:
The code is
SaveFileDialog objSaveDialog = new SaveFileDialog();
try
{
objSaveDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
objSaveDialog.Title = "Save to Text File";
//objSaveDialog.ShowDialog();
DialogResult dlgResult = objSaveDialog.ShowDialog();
objSaveDialog.Dispose();
if (dlgResult == DialogResult.OK)
{
string strSaveFilePath = objSaveDialog.FileName;
if (!string.IsNullOrEmpty(strSaveFilePath))
{
TextWriter myTxtWriter = new StreamWriter(strSaveFilePath, false);
for (int index = 0; index < 10000; index++)
{
myTxtWriter.WriteLine("sample text.....................................");
}
myTxtWriter.Flush();
myTxtWriter.Close();
myTxtWriter.Dispose();
}
}
}
finally
{
if (objSaveDialog != null)
{
objSaveDialog = null;
//((IDisposable)objSaveDialog).Dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您打开 shell 对话框时,许多代码会加载到您的进程中。您的计算机上安装的所有 shell 扩展处理程序。代码不是你写的。当您在“项目 + 属性”的“调试”选项卡中勾选“启用非托管代码调试”选项时,您可以看到它们在“输出”窗口中加载。
这些 shell 扩展处理程序行为不当并泄漏资源当然并不罕见。您可以使用 SysInternals 的 AutoRuns 实用程序来禁用它们。从非 Microsoft 编写的开始。
A lot of code gets loaded into your process when you open a shell dialog. All of the shell extension handlers installed on your machine. Code you didn't write. You can see them getting loaded in the Output window when you tick the "Enable unmanaged code debugging" option in the Project + Properties, Debug tab.
Having these shell extension handlers misbehave and leak resources is certainly not uncommon. You can use SysInternals' AutoRuns utility to disable them. Start with the ones not written by Microsoft.