diff --git a/dotnet/IEC61850forCSharp/Control.cs b/dotnet/IEC61850forCSharp/Control.cs
index 8604e3e..711542c 100644
--- a/dotnet/IEC61850forCSharp/Control.cs
+++ b/dotnet/IEC61850forCSharp/Control.cs
@@ -104,7 +104,7 @@ namespace IEC61850
///
/// Control object.
///
- public class ControlObject
+ public class ControlObject : IDisposable
{
[DllImport("iec61850", CallingConvention = CallingConvention.Cdecl)]
private static extern LastApplErrorInternal ControlObjectClient_getLastApplError(IntPtr self);
@@ -183,12 +183,6 @@ namespace IEC61850
ControlObjectClient_setCommandTerminationHandler(controlObject, intCommandTerminationHandler, controlObject);
}
- ~ControlObject ()
- {
- if (this.controlObject != System.IntPtr.Zero)
- ControlObjectClient_destroy(controlObject);
- }
-
///
/// Gets the control model.
///
@@ -443,6 +437,18 @@ namespace IEC61850
this.commandTerminationHandlerParameter = parameter;
}
+ protected virtual void Dispose(bool disposing) {
+ if (this.controlObject != System.IntPtr.Zero) {
+ ControlObjectClient_destroy (controlObject);
+ this.controlObject = System.IntPtr.Zero;
+ }
+ }
+
+ public void Dispose() {
+ Dispose (true);
+ }
+
+
}
}
diff --git a/dotnet/control/ControlExample.cs b/dotnet/control/ControlExample.cs
index 5504f31..7bce2ee 100644
--- a/dotnet/control/ControlExample.cs
+++ b/dotnet/control/ControlExample.cs
@@ -86,7 +86,10 @@ namespace control
Thread.Sleep(1000);
}
- con.Abort();
+ // has to be called before IedConnection dispose method!
+ control.Dispose();
+
+ con.Abort();
}
catch (IedConnectionException e)
{