How to prevent resource leaks while using Open/Save file dialog in c#
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();
}
}
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.
链接地址: http://www.djcxy.com/p/40546.html