void MsgQueueThread() { //only BTE_DISCONNECTION and BTE_CONNECTION change this state! addLog("thread about to start"); int hMsgQueue = 0; // IntPtr hBTevent = IntPtr.Zero; // allocate space to store the received messages byte[] msgBuffer = new byte[160];// Marshal.AllocHGlobal(160);// ITE_MESSAGE_SIZE); //MESSAGE _msg; WM_EVT_DATA _msg; try { //create msgQueueOptions MSGQUEUEOPTIONS msgQueueOptions = new MSGQUEUEOPTIONS(); msgQueueOptions.dwSize = (DWORD)Marshal.SizeOf(msgQueueOptions); msgQueueOptions.dwFlags = 0;// MSGQUEUE_NOPRECOMMIT; msgQueueOptions.dwMaxMessages = 10; msgQueueOptions.cbMaxMessage = MESSAGE_SIZE;// (DWORD)Marshal.SizeOf(ite_msg); msgQueueOptions.bReadAccess = ACCESS_READONLY; hMsgQueue = CreateMsgQueue(MESSAGE_QUEUE_NAME, ref msgQueueOptions); addLog("CreateMsgQueue=" + Marshal.GetLastWin32Error().ToString()); //6 = InvalidHandle if (hMsgQueue == 0) { addLog("Create MsgQueue failed"); throw new Exception("Create MsgQueue failed"); } Wait_Object waitRes = 0; //create a msg queue while (bRunThread) { // initialise values returned by ReadMsgQueue int bytesRead = 0; int msgProperties = 0; //block until message waitRes = (Wait_Object)WaitForSingleObject(hMsgQueue, 5000); if ((int)waitRes == -1) { int iErr = Marshal.GetLastWin32Error(); addLog("error in WaitForSingleObject=" + iErr.ToString()); //6 = InvalidHandle Thread.Sleep(1000); } switch (waitRes) { case Wait_Object.WAIT_OBJECT_0: //signaled //check event type and fire event //ReadMsgQueue entry //_msg = new MESSAGE(160); _msg = new WM_EVT_DATA(); bool success = ReadMsgQueue(hMsgQueue, // the open message queue _msg.buffer,// msgBuffer, // buffer to store msg _msg.size, // size of the buffer out bytesRead, // bytes stored in buffer -1, // wait forever out msgProperties); if (success) { // marshal the data read from the queue into a structure //ite_msg = (ITE_MESSAGE)Marshal.PtrToStructure(msgBuffer, typeof(ITE_MESSAGE)); addLog("msgqueue read: " + _msg.ToString());//Encoding.Unicode.GetString(ite_msg.msg, 0, bytesRead)); } else { addLog("ReadMsgQueue error: " + Marshal.GetLastWin32Error().ToString()); continue; //start a new while cirlce } //addLog("message received: " + ite_msg.ToString()); break; case Wait_Object.WAIT_ABANDONED: //wait has abandoned addLog("msg queue thread: WAIT_ABANDONED"); break; case Wait_Object.WAIT_TIMEOUT: //timed out addLog("msg queue thread: WAIT_TIMEOUT"); break; }//WaitRes }//while bRunThread } catch (ThreadAbortException ex) { addLog("msg queue thread ThreadAbortException: " + ex.Message + "\r\n" + ex.StackTrace); } catch (Exception ex) { addLog("msg queue thread exception: " + ex.Message + "\r\n" + ex.StackTrace); } finally { //Marshal.FreeHGlobal(msgBuffer); CloseMsgQueue(hMsgQueue); } addLog("msgqueue thread ended"); }
void MsgQueueThread() { //only BTE_DISCONNECTION and BTE_CONNECTION change this state! addLog("thread about to start"); int hMsgQueue = 0; // IntPtr hBTevent = IntPtr.Zero; // allocate space to store the received messages byte[] msgBuffer = new byte[160];// Marshal.AllocHGlobal(160);// ITE_MESSAGE_SIZE); //MESSAGE _msg; WM_EVT_DATA _msg; try { //create msgQueueOptions MSGQUEUEOPTIONS msgQueueOptions = new MSGQUEUEOPTIONS(); msgQueueOptions.dwSize = (DWORD)Marshal.SizeOf(msgQueueOptions); msgQueueOptions.dwFlags = 0; // MSGQUEUE_NOPRECOMMIT; msgQueueOptions.dwMaxMessages = 10; msgQueueOptions.cbMaxMessage = MESSAGE_SIZE; // (DWORD)Marshal.SizeOf(ite_msg); msgQueueOptions.bReadAccess = ACCESS_READONLY; hMsgQueue = CreateMsgQueue(MESSAGE_QUEUE_NAME, ref msgQueueOptions); addLog("CreateMsgQueue=" + Marshal.GetLastWin32Error().ToString()); //6 = InvalidHandle if (hMsgQueue == 0) { addLog("Create MsgQueue failed"); throw new Exception("Create MsgQueue failed"); } Wait_Object waitRes = 0; //create a msg queue while (bRunThread) { // initialise values returned by ReadMsgQueue int bytesRead = 0; int msgProperties = 0; //block until message waitRes = (Wait_Object)WaitForSingleObject(hMsgQueue, 5000); if ((int)waitRes == -1) { int iErr = Marshal.GetLastWin32Error(); addLog("error in WaitForSingleObject=" + iErr.ToString()); //6 = InvalidHandle Thread.Sleep(1000); } switch (waitRes) { case Wait_Object.WAIT_OBJECT_0: //signaled //check event type and fire event //ReadMsgQueue entry //_msg = new MESSAGE(160); _msg = new WM_EVT_DATA(); bool success = ReadMsgQueue(hMsgQueue, // the open message queue _msg.buffer, // msgBuffer, // buffer to store msg _msg.size, // size of the buffer out bytesRead, // bytes stored in buffer -1, // wait forever out msgProperties); if (success) { // marshal the data read from the queue into a structure //ite_msg = (ITE_MESSAGE)Marshal.PtrToStructure(msgBuffer, typeof(ITE_MESSAGE)); addLog("msgqueue read: " + _msg.ToString()); //Encoding.Unicode.GetString(ite_msg.msg, 0, bytesRead)); } else { addLog("ReadMsgQueue error: " + Marshal.GetLastWin32Error().ToString()); continue; //start a new while cirlce } //addLog("message received: " + ite_msg.ToString()); break; case Wait_Object.WAIT_ABANDONED: //wait has abandoned addLog("msg queue thread: WAIT_ABANDONED"); break; case Wait_Object.WAIT_TIMEOUT: //timed out addLog("msg queue thread: WAIT_TIMEOUT"); break; } //WaitRes } //while bRunThread } catch (ThreadAbortException ex) { addLog("msg queue thread ThreadAbortException: " + ex.Message + "\r\n" + ex.StackTrace); } catch (Exception ex) { addLog("msg queue thread exception: " + ex.Message + "\r\n" + ex.StackTrace); } finally { //Marshal.FreeHGlobal(msgBuffer); CloseMsgQueue(hMsgQueue); } addLog("msgqueue thread ended"); }