- .NET API: added Dispose method to IecConnection and ReportControlBlock classes

pull/6/head
Michael Zillgith 10 years ago
parent c8e08597b3
commit d9c8d9cc1b

@ -259,12 +259,31 @@ namespace IEC61850
connection = IedConnection_create (); connection = IedConnection_create ();
} }
/// <summary>
/// Releases all resource used by the <see cref="IEC61850.Client.IedConnection"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="IEC61850.Client.IedConnection"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="IEC61850.Client.IedConnection"/> in an unusable state. After
/// calling <see cref="Dispose"/>, you must release all references to the <see cref="IEC61850.Client.IedConnection"/>
/// so the garbage collector can reclaim the memory that the <see cref="IEC61850.Client.IedConnection"/> was occupying.</remarks>
public void Dispose()
{
if (connection != IntPtr.Zero) {
cleanupRCBs ();
IedConnection_destroy (connection);
connection = IntPtr.Zero;
}
}
~IedConnection () ~IedConnection ()
{ {
Console.WriteLine ("IedConnection destructor invoked"); if (connection != IntPtr.Zero) {
if (connection != IntPtr.Zero) cleanupRCBs ();
IedConnection_destroy(connection);
Console.WriteLine ("IedConnection destructor finished"); IedConnection_destroy (connection);
}
} }
public IsoConnectionParameters GetConnectionParameters () public IsoConnectionParameters GetConnectionParameters ()

@ -203,7 +203,6 @@ namespace IEC61850
private void internalReportHandler (IntPtr parameter, IntPtr report) private void internalReportHandler (IntPtr parameter, IntPtr report)
{ {
Console.WriteLine ("called internalReportHandler");
try { try {
if (this.report == null) if (this.report == null)
@ -227,13 +226,25 @@ namespace IEC61850
this.objectReference = objectReference; this.objectReference = objectReference;
} }
~ReportControlBlock() internal void DisposeInternal()
{ {
Console.WriteLine ("Destructor invoked"); IedConnection_uninstallReportHandler(connection, objectReference);
//IedConnection_uninstallReportHandler(connection, objectReference); }
//this.iedConnection = null;
Console.WriteLine ("Destructor finished"); /// <summary>
} /// Releases all resource used by the <see cref="IEC61850.Client.ReportControlBlock"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="IEC61850.Client.ReportControlBlock"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="IEC61850.Client.ReportControlBlock"/> in an unusable state.
/// After calling <see cref="Dispose"/>, you must release all references to the
/// <see cref="IEC61850.Client.ReportControlBlock"/> so the garbage collector can reclaim the memory that the
/// <see cref="IEC61850.Client.ReportControlBlock"/> was occupying.</remarks>
public void Dispose()
{
DisposeInternal ();
iedConnection.RemoveRCB (this);
}
public string GetObjectReference () public string GetObjectReference ()
{ {

@ -24,6 +24,7 @@ using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using IEC61850.Common; using IEC61850.Common;
using System.Collections.Generic;
namespace IEC61850 namespace IEC61850
{ {
@ -31,10 +32,38 @@ namespace IEC61850
{ {
public partial class IedConnection public partial class IedConnection
{ {
private List<ReportControlBlock> activeRCBs = null;
private void cleanupRCBs()
{
if (activeRCBs != null) {
foreach (ReportControlBlock rcb in activeRCBs) {
rcb.DisposeInternal ();
}
}
}
public ReportControlBlock GetReportControlBlock (string rcbObjectReference) public ReportControlBlock GetReportControlBlock (string rcbObjectReference)
{ {
return new ReportControlBlock (rcbObjectReference, this, connection); var newRCB = new ReportControlBlock (rcbObjectReference, this, connection);
if (activeRCBs == null)
activeRCBs = new List<ReportControlBlock> ();
activeRCBs.Add (newRCB);
return newRCB;
} }
internal void RemoveRCB(ReportControlBlock rcb) {
if (activeRCBs != null) {
activeRCBs.Remove (rcb);
}
}
} }
public enum ReasonForInclusion public enum ReasonForInclusion

@ -43,6 +43,10 @@ namespace authenticate
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -91,7 +91,10 @@ namespace control
catch (IedConnectionException e) catch (IedConnectionException e)
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }

@ -72,7 +72,10 @@ namespace datasets
catch (IedConnectionException e) catch (IedConnectionException e)
{ {
Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString()); Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString());
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }

@ -72,7 +72,10 @@ namespace example1
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
System.Threading.Thread.Sleep(2000); System.Threading.Thread.Sleep(2000);
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -39,6 +39,8 @@ namespace example2
Console.WriteLine("IED connection excepion: " + e.Message); Console.WriteLine("IED connection excepion: " + e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -43,6 +43,9 @@ namespace example3
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -84,6 +84,9 @@ namespace files
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -118,6 +118,8 @@ namespace model_browsing
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -104,6 +104,8 @@ namespace report_new_dataset
Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString()); Console.WriteLine(e.Message + " reason: " + e.GetIedClientError().ToString());
} }
// release all resources - do NOT use the object after this call!!
con.Dispose ();
} }
} }
} }

@ -138,9 +138,18 @@ namespace reporting
Thread.Sleep(10); Thread.Sleep(10);
} }
/* Dispose the RCBs when you no longer need them */
rcb1.Dispose();
rcb2.Dispose();
rcb3.Dispose();
con.Abort (); con.Abort ();
con.Dispose();
} catch (IedConnectionException e) { } catch (IedConnectionException e) {
Console.WriteLine ("Error: " + e.Message); Console.WriteLine ("Error: " + e.Message);
con.Dispose ();
} }
} }

Loading…
Cancel
Save