BTool.TxDataOutThread.TaskThread C# (CSharp) Method

TaskThread() private method

private TaskThread ( object threadData ) : void
threadData object
return void
        private void TaskThread(object threadData)
        {
            try
            {
                bool flag = false;
                threadCtrl.Init();
                threadCtrl.RunningThread = true;
                SharedObjects.Log.Write(Logging.MsgType.Debug, "TxDataOutThread", "Starting Thread");
                while (!flag)
                {
                    if (threadCtrl.ExitThread)
                        break;
                    if (threadCtrl.PauseThread)
                    {
                        threadCtrl.IdleThread = true;
                        SharedObjects.Log.Write(Logging.MsgType.Debug, "TxDataOutThread", "Pausing Thread");
                        threadCtrl.EventPause.WaitOne();
                        threadCtrl.IdleThread = false;
                        if (threadCtrl.ExitThread)
                            break;
                    }
                    if (!stopWaitMsg)
                    {
                        switch (WaitHandle.WaitAny(
                            new WaitHandle[3]
                                {
                                    threadCtrl.EventExit,
                                    threadCtrl.EventPause,
                                    dataQ.qDataReadyEvent
                                }))
                        {
                            case 0:
                                flag = true;
                                break;
                            case 1:
                                threadCtrl.EventPause.Reset();
                                SharedObjects.Log.Write(Logging.MsgType.Debug, "TxDataOutThread", "Resuming Thread");
                                break;
                            case 2:
                                dataQ.qDataReadyEvent.Reset();
                                QueueDataReady();
                                break;
                            default:
                                flag = true;
                                break;
                        }
                        continue;
                    }

                    switch (WaitHandle.WaitAny(
                        new WaitHandle[3]
                                {
                                    threadCtrl.EventExit,
                                    threadCtrl.EventPause,
                                    stopWaitSuccessEvent
                                }, new TimeSpan(0, 0, 0, 40)))
                    {
                        case 0:
                            flag = true;
                            break;
                        case 1:
                            threadCtrl.EventPause.Reset();
                            SharedObjects.Log.Write(Logging.MsgType.Debug, "TxDataOutThread", "Resuming Thread");
                            break;
                        case 2:
                            stopWaitSuccessEvent.Reset();
                            stopWaitEvent = (HCIStopWait.StopWaitEvent)null;
                            stopWaitMsg = false;
                            break;
                        case 258:
                            if (DeviceRxStopWaitCallback != null)
                                DeviceRxStopWaitCallback(false, (HCIStopWait.StopWaitEvent)null);
                            if (stopWaitEvent != null)
                            {
                                string msg = "Message Response Timeout\nName = " + devUtils.GetOpCodeName((ushort)stopWaitEvent.TxOpcode) + "\nOpcode = 0x" + ((ushort)stopWaitEvent.TxOpcode).ToString("X4") + "\nTx Time = " + stopWaitEvent.TxTime + "\n";
                                if (DisplayMsgCallback != null)
                                    DisplayMsgCallback(SharedAppObjs.MsgType.Error, msg);
                                if (stopWaitEvent.Callback == null)
                                {
                                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                                    ClearTxQueueQuestion();
                                }
                                if (ShowProgressCallback != null)
                                    ShowProgressCallback(false);
                                if (stopWaitEvent.Callback != null)
                                    stopWaitEvent.Callback(false, stopWaitEvent.CmdName);
                            }
                            stopWaitEvent = (HCIStopWait.StopWaitEvent)null;
                            stopWaitMsg = false;
                            break;
                        default:
                            flag = true;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, "Task Thread Problem.\n" + ex.Message + "\nTxDataOutThread\n");
            }
            SharedObjects.Log.Write(Logging.MsgType.Debug, "TxDataOutThread", "Exiting Thread");
            threadCtrl.Exit();
        }