R
Rudi
Hello,
following problem: At program end or release an assembly a serial device
should get a final exit sequence. How can I do this?
With Dispose() it's no problem, but this assembly is used in a com
interop dll and it must be guaranteed, that the final sequence is send
to the serial device, when the calling application dont send Close() or
Dispose().
How can we solve this problem? Please see last 3 lines of sample code?
I tried many ways for now, but also with IDisposable it does'nt work.
kind regards, Rudi
8<---------------------------------------------------------------------
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}
8<---------------------------------------------------------------------
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}
following problem: At program end or release an assembly a serial device
should get a final exit sequence. How can I do this?
With Dispose() it's no problem, but this assembly is used in a com
interop dll and it must be guaranteed, that the final sequence is send
to the serial device, when the calling application dont send Close() or
Dispose().
How can we solve this problem? Please see last 3 lines of sample code?
I tried many ways for now, but also with IDisposable it does'nt work.
kind regards, Rudi
8<---------------------------------------------------------------------
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}
8<---------------------------------------------------------------------
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication3
{
class Test : IDisposable
{
SerialPort sio = new SerialPort();
int loopCtr = 0;
private bool disposeFlag = true;
public Test(int lpCtr)
{
loopCtr = lpCtr;
Console.WriteLine("constructor " + loopCtr);
sio.PortName = "COM1";
sio.Open();
if (sio.IsOpen)
{
sio.Write("DEVICE INIT");
Console.WriteLine("sio: INIT OK");
}
}
public void Say(string text)
{
Console.WriteLine(text);
}
~Test()
{
Dispose(false);
Console.WriteLine("destructor " + loopCtr);
}
public void Dispose()
{
Dispose(true);
}
internal void DoExitSequence()
{
if (sio.IsOpen)
{
sio.Write("DEVICE EXIT");
Console.WriteLine("sio: EXIT OK");
}
else
Console.WriteLine("sio: EXIT failed");
sio.Close();
GC.KeepAlive(sio);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
GC.SuppressFinalize(this);
}
}
if (disposeFlag)
{
DoExitSequence();
disposeFlag = false;
}
}
}
class Program
{
static void DoTest(int i)
{
Test t = new Test(i);
using(t)
{
t.Say("Test " + i);
}
//t.Dispose();
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop " + i);
DoTest(i);
/*
GC.Collect();
Thread.Sleep(100);
GC.Collect();
*/
}
Console.ReadLine();
Test t = new Test(99);
t.Say("sio closed?");
//t.Dispose(); destructor should clean up
}
}
}