public void OnStart() { isConnecting = true; var t1 = Task.Run(() => { while (isConnecting) { if (Interlocked.Exchange(ref inTimer, 1) == 0) { MQHandler queue = new MQHandler(); string rcvd; do { rcvd = queue.ReceiveMessage(IntData.QUEUE_NAME, IntData.QUEUE_NAME + " Message");//Dequeue the query if (rcvd != "") { ThreadHandler.ThreadLocker();//Pause the program when thread is locked ExecuteSQL(rcvd); } } while (rcvd != ""); Interlocked.Exchange(ref inTimer, 0); } Thread.Sleep(1000); } }); }