I have a class which is in charge for error handling. I would like to execute a process, if the destructor is called. But sadly the process would not start. The new processes call an exe with some arguments, which should send an email.
Why does this not work?
~ErrorH()
{
if ((int)e > 0)
SendErrorMail();
}
private void SendErrorMail()
{
if (File.Exists("C:\\Program Files (x86)\\MailSend\\MailSend.exe"))
{
ProcessStartInfo mailsend = new ProcessStartInfo();
mailsend.FileName = "C:\\Program Files (x86)\\MailSend\\MailSend.exe";
mailsend.Arguments = "…";
Process.Start(mailsend);
}
}
If I execute the SendErrorMail function for example in the constructor, everything works fine. If I look at the debugger it seems like I reach the Process.Start(mailsend); command.
What went wrong? How could I fix this?
Edit
Ok now I have used the IDisposable method. It works fine, but does is use it correctly?
class ErrorH : IDisposable
{
private bool disposed = false;
...
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool desposing)
{
if(!this.disposed)
if ((int)e > 0)
SendErrorMail();
disposed = true;
}
In Program I use:
using (Parameter p = new Parameter(args[0]))
{
...
}
The class ErrorH inheritance from Parameter.
Greetz
Rather than using destructors, which cannot be guaranteed to be called at current time, use interface
System.IDisposable.Usually, it is a bad practice to use destructors in C# like C++, for example. Since we cannot determine the specific time the objects will be destroyed by Garbage Collector (GC), C# provides
IDisposablewhich has a single methodDispose, that you can call it either explicitly when you finish using the object, or implicitly byusingblock.