From 0c09385462cd992372475baf9bf670e512dea333 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Sat, 28 Oct 2017 15:52:49 +0200 Subject: [PATCH] - .NET API: ControlObject implements IDisposable interface --- dotnet/IEC61850forCSharp/Control.cs | 20 +++++++++++++------- dotnet/control/ControlExample.cs | 5 ++++- 2 files changed, 17 insertions(+), 8 deletions(-) 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) {