Search code examples
c#.netwinformsio

how to acces SerialPort.Flush() and SerialPort.Finalize() functions


I was just browsing msdn and found this page. I did never see the functions SerialPort.Flush() or SerialPort.Finalize() before. So I tried to use those functions, but I'm getting an error.

I added the System.IO.Ports namespace, but I get the following error on the Finalize() function:

Cannot access protected member 'object.~Object()' via a qualifier of type 'System.IO.Ports.SerialPort'; the qualifier must be of type 'STP_Design.SerialCom' (or derived from it)

and i get the following error on the Flush() function:

'System.IO.Ports.SerialPort' does not contain a definition for 'Flush' and no extension method 'Flush' accepting a first argument of type 'System.IO.Ports.SerialPort' could be found (are you missing a using directive or an assembly reference?)

I guess I'm accessing the finalizing function with a wrong approach (and I must not access it at all probably) but I'm really wondering what about the Flush() function.

I used something like this:

    private void test()
    {
        SerialPort s1 = new SerialPort();
        s1.PortName = "COM1";
        s1.Open();
        thread.Sleep(200);
        s1.WriteLine("test");
        s1.Flush();
        s1.Close();
        thread.Sleep(200);
        s1.Finalize();
    }

Any insights here?

EDIT: Got the same problem with the SerialPort.Dispose(boolean) function The optional boolean value is not accesseble too...


Solution

  • http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx : no Flush. You are looking at the .Net Micro Framework, which apparently does have a Flush

    Finalizers are called by GC at the end of garbage collection. They aren't externally accessible by user code, nor should they be explicitly called.