Hi Chris,
Thanks for your attention
Well, I read your suggestion, but I don't have any idea of how I can do it.
I think if I exit only this event (the thread in discussion) I'll be able to
quit the application. I don't have any problem to do it if this thread is not
running, so the other threads are finalizing ok.
I'll put the code here, please help me giving one light if you can ...
// Listener thread
void WaitThreadProc()
{
// Open our message queue for reading
MSGQUEUEOPTIONS opt = new MSGQUEUEOPTIONS();
opt.bReadAccess = 1;
IntPtr hQueue = CreateMsgQueue( "PowerEventWaitQueue", opt);
// Start waiting for Message Queue to fill up, while checking for a
termination request
while ( WaitForSingleObject( m_hEvtQuit, 0 ) != WAIT_OBJECT_0 )
{
// Did queue get some data?
if ( WaitForSingleObject(hQueue, 100) == WAIT_OBJECT_0 )
{
// Yes, let's read it
// 1024 byte buffer should be well enough.
POWER_BROADCAST pb = new POWER_BROADCAST(1024);
int nRead, Flags;
ReadMsgQueue(hQueue, pb.Data, pb.Data.Length, out nRead, 0, out Flags);
// Is this an event we wanted?
if ((pb.Message & PowerEventType.PBT_TRANSITION) ==
PowerEventType.PBT_TRANSITION)
{
if (( pb.Flags & PowerState.POWER_STATE_ON ) ==
PowerState.POWER_STATE_ON )
{
//PDA has awaken from suspend mode
//Check if port is closed
if (port.Enabled == false)
{
try
{
Cursor.Current=Cursors.WaitCursor;
//Wait 5 seconds before we open the port
//If you don't you will get CreateFile error 55.
//I am guess that the bluetooth port has not initialized by
//the PDA
Thread.Sleep(5000);
//Set warning flag to tell port is not closed
this.portClosedWarningFlag=false;
port.Enabled = true;
//Close and reopen the port
this.portClosedWarningFlag=true;
port.Enabled = false;
//Wait another 5 seconds before opening the port
//This is done to make sure that the port has
//sufficient time to close.
Thread.Sleep(5000);
this.portClosedWarningFlag=false;
port.Enabled = true;
}
catch(Exception ex)
{
//MessageBox.Show("WaitThread reopening port: " +ex.ToString());
}
finally
{
Cursor.Current=Cursors.Default;
}
}
}
else if (( pb.Flags & PowerState.POWER_STATE_SUSPEND )
== PowerState.POWER_STATE_SUSPEND )
{
//PDA is going into Suspend mode
try
{
//Check if the port is open.
if (port.Enabled)
{
//Set the close port warning flag
this.portClosedWarningFlag=true;
//Close and dispose of the port
port.Enabled = false;
port.Dispose();
}
}
catch(Exception ex)
{
MessageBox.Show("WaitThread reopening port: " +ex.ToString());
}
}
}
}
}
CloseHandle(hQueue);
CloseHandle(m_hEvtQuit);
}