Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Safe handle has been closed appears on program exit

Safe handle has been closed appears on program exit

Scheduled Pinned Locked Moved C#
helpdebuggingcsharpcomdata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hello, i sometimes get the "Safe handle has been closed" exception when i CLOSE my application (in debug mode). When i run it from the exe and try to close the app i simple get the "program has stopped working" error. I use a com port to communicate with an external device. I hear it has something to do with a .net bug but is there anything i can do in the Form_closing event perhaps or somewhere else to stop it from showing when i close my program. How could i handle that exception? Heres the stack trace: System.ObjectDisposedException was unhandled Message="Safe handle has been closed" Source="mscorlib" ObjectName="" StackTrace: at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle) at System.Threading.EventWaitHandle.Set() at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) InnerException: My idea was the following: (to make sure that the communication thread does not send anything else when user wants to exit) Form_Closing event { signal_comm_thread loop while(getSignal_from_comm_thread) } Comm_thread { if(signal_received) signal_Form_Closing event break; } But the program wont close at all this way :( Tried to use ManualResetEvents, public static variables for the signaling but nothing helps. Only way the program closes is if i add a "MessageBox.Show" in Form_Closing event after signaling comm_thread. How can that help? TY

    A S 2 Replies Last reply
    0
    • Y yeah1000

      Hello, i sometimes get the "Safe handle has been closed" exception when i CLOSE my application (in debug mode). When i run it from the exe and try to close the app i simple get the "program has stopped working" error. I use a com port to communicate with an external device. I hear it has something to do with a .net bug but is there anything i can do in the Form_closing event perhaps or somewhere else to stop it from showing when i close my program. How could i handle that exception? Heres the stack trace: System.ObjectDisposedException was unhandled Message="Safe handle has been closed" Source="mscorlib" ObjectName="" StackTrace: at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle) at System.Threading.EventWaitHandle.Set() at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) InnerException: My idea was the following: (to make sure that the communication thread does not send anything else when user wants to exit) Form_Closing event { signal_comm_thread loop while(getSignal_from_comm_thread) } Comm_thread { if(signal_received) signal_Form_Closing event break; } But the program wont close at all this way :( Tried to use ManualResetEvents, public static variables for the signaling but nothing helps. Only way the program closes is if i add a "MessageBox.Show" in Form_Closing event after signaling comm_thread. How can that help? TY

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      I've seen similar things happens when attempting to close a serial port read thread at form close. What I do now is allow the reader thread to close the port and wait in the UI thread until the reader thread has exited. Like this:

      private void ReadWriteThreadProc() {
        continueLooping = 1;
        try {
          while (continueLooping != 0) {
            try {
              received = com.Read(buffer, 0, buffer.Length);
              // process buffer etc.
            } catch (TimeoutException) {
              // do timeout related stuff
            }
          }
        } finally {
          com.Close();
        }
      }
      
      public void CloseCom() {
        Interlocked.Exchange(ref continueLooping, 0);
        ReadWriteThread.Join();
      }
      

      The timeout is rather crucial as it ensures that the exit condition is polled. Alan.

      1 Reply Last reply
      0
      • Y yeah1000

        Hello, i sometimes get the "Safe handle has been closed" exception when i CLOSE my application (in debug mode). When i run it from the exe and try to close the app i simple get the "program has stopped working" error. I use a com port to communicate with an external device. I hear it has something to do with a .net bug but is there anything i can do in the Form_closing event perhaps or somewhere else to stop it from showing when i close my program. How could i handle that exception? Heres the stack trace: System.ObjectDisposedException was unhandled Message="Safe handle has been closed" Source="mscorlib" ObjectName="" StackTrace: at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle) at System.Threading.EventWaitHandle.Set() at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) InnerException: My idea was the following: (to make sure that the communication thread does not send anything else when user wants to exit) Form_Closing event { signal_comm_thread loop while(getSignal_from_comm_thread) } Comm_thread { if(signal_received) signal_Form_Closing event break; } But the program wont close at all this way :( Tried to use ManualResetEvents, public static variables for the signaling but nothing helps. Only way the program closes is if i add a "MessageBox.Show" in Form_Closing event after signaling comm_thread. How can that help? TY

        S Offline
        S Offline
        supercat9
        wrote on last edited by
        #3

        Are you using a serial port, or a USB-to-serial converter? The SerialPort object in .Net does not behave well at all if a port is open on a USB-to-serial converter that is unplugged. I don't know any good libraries, but I'm sure they're out there. For my application, I coded a silly little helper application which acts as a bridge between a TCP socket and a .net SerialPort object; if the SerialPort object dies, the helper application quits without jinxing the main app.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups