I write a class program with IDisposable. Now I want to dispose my managed and unmanaged memory. I am confused about the resources which are belong to managed and unmanaged memory.
I give class example:-
namespace WF2006toI4scada
{
public class HelperClass : IHelperClass, IDisposable
{
static string filename;
static string aspfilename;
private StreamReader file;
static Excel.Workbook xlWorkBook;
static Excel.Worksheet xlWorkSheet;
static Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
private IDictionary<int, List<string>> vbsComponents;
private IService services;
private bool IsDisposed = false;
private static int totalNoofVBSElement = 0;
private string projectsettinginifile;
private static HtmlDocument document2;
private readonly ILogger loggerobject;
public HelperClass()
{
loggerobject = Logger.Instance;
application.Interactive = false;
}
public IDictionary<int, List<string>> VbsComponents { get => VbsComponents1; set => VbsComponents1 = value; }
public string Filepath { get => filename; }
public IDictionary<int, List<string>> VbsComponents1 { get => vbsComponents; set => vbsComponents = value; }
public IService Services { get => services; set => services = value; }
public string Projectsettinginifile { get => projectsettinginifile; set => projectsettinginifile = value; }
public string aspFilepath { get => aspfilename; }
public HtmlDocument htmlDocument { get => document2; set => document2 = value; }
public async Task ReadASPFileAsync()
{
try
{
try
{
loggerobject.debug("Browsing for ASP file started");
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "asp File |*.asp;*,asp";
if (ofd.ShowDialog() == DialogResult.OK)
{
string extn = Path.GetExtension(ofd.FileName);
if (extn.Equals(".asp"))
{
aspfilename = ofd.FileName;
file = new System.IO.StreamReader(aspfilename);
}
else
{
throw new FileNotFoundException("File not found", ".asp");
}
}
}
catch (FileNotFoundException ex)
{
new CustomSytemException("Caught this Error in ReadASPFileAsync function", DateTime.Now, ex);
loggerobject.exception(ex.Message, new SystemException("File not found asp" , ex));
}
await Task.Run(() =>
{
try
{
loggerobject.debug("Reading ASP file started");
if (!string.IsNullOrEmpty(aspfilename))
{
document2 = new HtmlDocument();
document2.Load(aspfilename);
if (object.ReferenceEquals(null, document2))
{
throw new FileReadingException("ASP file reading error", DateTime.Now);
}
}
}
catch (FileReadingException ex)
{
new CustomSytemException("Caught this Error in ReadASPFileAsync function", DateTime.Now, ex);
loggerobject.exception(ex.Message, new SystemException("File not found asp", ex));
}
catch(FileNotFoundException ex)
{
new CustomSytemException("Caught this Error in ReadASPFileAsync function", DateTime.Now, ex);
loggerobject.exception(ex.Message, new SystemException("ASP file reading error", ex));
}
});
}
catch (CustomSytemException ew)
{
MessageBox.Show(ew.ErrorTimeStamp + "\nErrror:" + ew.Message + "\n Inner strace:" + ew.InnerException.StackTrace + "\n Inner messege:"
+ ew.InnerException.Message + "\n" + ew.CauseOfError);
}
}
/// <summary>
/// This method use to read the .vbs file
/// </summary>
/// <returns></returns>
public async Task ReadVbsFileAsync()
{
loggerobject.debug("Reading VBS file started");
bool opreation = false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "vbs File |*.vbs;*,vbs";
if (ofd.ShowDialog() == DialogResult.OK)
{
string extn = Path.GetExtension(ofd.FileName);
if (extn.Equals(".vbs"))
{
filename = ofd.FileName;
if (filename != "")
{
await Task.Run(() =>
{
try
{
string vbsfilename = Path.GetFileName(filename);
object missing = System.Reflection.Missing.Value;
application.Visible = true;
try
{
loggerobject.debug("VBS file to Excel converted started");
application.Workbooks.OpenText(Filename: filename, StartRow: 1,
DataType: Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Tab: false);
if(application.Workbooks.Count == 0)
{
throw new FileNotFoundException("File not found", ".vbs");
}
xlWorkBook = application.Workbooks[1];
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.ActiveSheet;
}
catch(FileNotFoundException ex)
{
loggerobject.exception(ex.Message, new SystemException("File not found .vbs", ex));
throw new CustomSytemException("Reading VBS file error", DateTime.Now,ex);
}
/*
Triming process for worksheet
*/
try
{
loggerobject.debug("Excel Triming process started");
xlWorkSheet.Trim();
xlWorkSheet.TrimSpecialCharacter(Projectsettinginifile, Services);
opreation = true;
if(!opreation)
{
new InvalidOperationException("X1worksheet file triming operation failed");
}
}
catch (InvalidOperationException ex)
{
loggerobject.exception(ex.Message, new SystemException("X1worksheet file triming operation failed", ex));
throw new CustomSytemException("Error in triming operation of xlWorkSheet ", DateTime.Now, ex);
}
finally
{
opreation = false;
}
/*
Excel format is generated
create dictionary format tooltip and plcsignal
*/
try
{
loggerobject.debug("Create dictionary of all VBS elemnts");
VbsComponents = (IDictionary<int, List<string>>)xlWorkSheet.CreateDictionary();
if ( object.ReferenceEquals(null, VbsComponents))
{
throw new FileReadingException("Dictionary not generated from VBS file", DateTime.Now);
}
}
catch(FileReadingException ex)
{
loggerobject.exception(ex.Message, new SystemException("Dictionary not generated from VBS file", ex));
throw new CustomSytemException("Dictionary not generated from VBS file", DateTime.Now,ex);
}
xlWorkBook.Close();
}
catch (CustomSytemException ew)
{
MessageBox.Show(ew.ErrorTimeStamp + "\nErrror:" + ew.Message + "\n Inner strace:" + ew.InnerException.StackTrace + "\n Inner messege:"
+ew.InnerException.Message + "\n" + ew.CauseOfError );
application.Quit();
}
finally
{
application.Quit();
}
});
}
}
}
}
public virtual void Dispose()
{
if (!IsDisposed)
{
IsDisposed = true;
file.Dispose();
application.Quit();
Marshal.ReleaseComObject(application); // only if you are the only holder of application
}
}
}
}
What are managed and unmanaged resources here and how to dispose them.
Could you please help me out to avoid memory leak.