Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I don't know what part of the code is throwing the exception. I have a USB autodetect feature in my application.

The exception thrown is as below:
safe handle closed

   at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFile, NativeOverlapped* lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean bWait)
   at System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Please help!
Posted
Updated 18-Jan-11 19:02pm
v2

It is best to detect those events and stop/suspend the thread(s) when they occur. Have a look at the links below about detecting these events.

Handling OS events like Sleep, Stand-by, Hibernate, Power Status Changed, Low Battery, Critial Suspend, AC Power, Battery Power, Battery Life etc... in Windows XP and Vista.[^]

Use System Events To Protect Your Application Data[^]

Good luck!
 
Share this answer
 
Comments
vidya_01 19-Jan-11 4:41am    
Thanks so much! The application in the below link works great! but I'm looking for a similar one in C#. Or do you any work around ?
Espen Harlinn 19-Jan-11 5:52am    
5+ Good answer, backed up by informative links
The 2nd link was in C#. Of course, the code in the article isn't going to be an exact fit for your needs, but being a programmer, you shouldn't have any issues extracting and adapting the code you need from it, right?
 
Share this answer
 
Comments
vidya_01 19-Jan-11 5:47am    
I went thru that. I have implemented the following similar code in my application:
private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
//The system is going into or resuming from standby
if (e.Mode == Microsoft.Win32.PowerModes.Suspend)
{
mLog.Info("System is going into standby, FST must shutdown");
clsMessageBroker.Shutdown();

}
else if (e.Mode == Microsoft.Win32.PowerModes.Resume)
{
mLog.Info("System is resuming from standby");

}
But this is not helping. I doubt the autodetect has got to do soemthing with the problem. It has a lot of handlers called thru the wndprc method, but i'm not sure how they r getting closed or which of htem is throwing the exception which is leading to the crash.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900