Index: trunk/src/VBox/Additions/x11/VBoxClient/draganddrop.cpp
===================================================================
--- trunk/src/VBox/Additions/x11/VBoxClient/draganddrop.cpp	(revision 46147)
+++ trunk/src/VBox/Additions/x11/VBoxClient/draganddrop.cpp	(revision 46377)
@@ -81,5 +81,5 @@
 # define DO(s) RTPrintf s
 #else
-# define DO(s) do {} while(0)
+# define DO(s) do {} while (0)
 //# define DO(s) Log s
 #endif
@@ -595,5 +595,5 @@
         XChangeProperty(m_pDisplay, m_proxyWin, xAtom(XA_XdndAware), XA_ATOM, 32, PropModeReplace,
                         reinterpret_cast<unsigned char*>(&ver), 1);
-    } while(0);
+    } while (0);
 
     m_state = Initialized;
@@ -1137,5 +1137,5 @@
             }
         }
-    } while(tries--);
+    } while (tries--);
     if (clme)
     {
@@ -1503,4 +1503,6 @@
 }
 
+#ifdef VBOX_WITH_DRAG_AND_DROP_GH
+
 bool DragAndDropService::waitForXMsg(XEvent &ecm, int type, uint32_t uiMaxMS /* = 100 */)
 {
@@ -1530,8 +1532,10 @@
 //            return false;
     }
-    while(RTTimeProgramMilliTS() - uiStart < uiMaxMS);
+    while (RTTimeProgramMilliTS() - uiStart < uiMaxMS);
 
     return false;
 }
+
+#endif
 
 void DragAndDropService::clearEventQueue()
@@ -1574,5 +1578,5 @@
             RT_ZERO(e);
             if (m_eventQueue.isEmpty())
-                rc = RTSemEventWait(m_hEventSem, 50);
+                rc = RTSemEventWait(m_hEventSem, RT_INDEFINITE_WAIT);
             if (!m_eventQueue.isEmpty())
             {
@@ -1665,6 +1669,6 @@
                 }
             }
-        } while(!ASMAtomicReadBool(&m_fSrvStopping));
-    } while(0);
+        } while (!ASMAtomicReadBool(&m_fSrvStopping));
+    } while (0);
 
     LogRelFlowFunc(("returning %Rrc\n", rc));
@@ -1699,5 +1703,5 @@
                             0, RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE,
                             "X11-NOTIFY");
-    } while(0);
+    } while (0);
 
     /* Cleanup on failure */
@@ -1712,5 +1716,6 @@
     /* Mark that we are stopping. */
     ASMAtomicWriteBool(&m_fSrvStopping, true);
-
+    RTSemEventSignal(m_hEventSem);
+  
     if (m_pDisplay)
     {
@@ -1727,9 +1732,9 @@
                 DO(("DnD_TERM: error sending xevent\n"));
     }
+    
+    /* We cannot signal the m_hHGCMThread as it is most likely waiting in vbglR3DoIOCtl() */
     /* Wait for our event threads to stop. */
-//    if (m_hX11Thread)
-//        RTThreadWait(m_hX11Thread, RT_INDEFINITE_WAIT, 0);
-//    if (m_hHGCMThread)
-//        RTThreadWait(m_hHGCMThread, RT_INDEFINITE_WAIT, 0);
+    if (m_hX11Thread)
+        RTThreadWait(m_hX11Thread, RT_INDEFINITE_WAIT, NULL);
     /* Cleanup */
     /* todo: This doesn't work. The semaphore was interrupted by the user
@@ -1749,5 +1754,5 @@
 {
     AssertPtrReturn(pvUser, VERR_INVALID_PARAMETER);
-    DragAndDropService *pSrv = static_cast<DragAndDropService*>(pvUser);
+    DragAndDropService *pThis = static_cast<DragAndDropService*>(pvUser);
     DnDEvent e;
     do
@@ -1759,10 +1764,10 @@
         if (RT_SUCCESS(rc))
         {
-            pSrv->m_eventQueue.append(e);
-            rc = RTSemEventSignal(pSrv->m_hEventSem);
+            pThis->m_eventQueue.append(e);
+            rc = RTSemEventSignal(pThis->m_hEventSem);
             if (RT_FAILURE(rc))
                 return rc;
         }
-    } while(!ASMAtomicReadBool(&pSrv->m_fSrvStopping));
+    } while (!ASMAtomicReadBool(&pThis->m_fSrvStopping));
 
     return VINF_SUCCESS;
@@ -1773,5 +1778,5 @@
 {
     AssertPtrReturn(pvUser, VERR_INVALID_PARAMETER);
-    DragAndDropService *pSrv = static_cast<DragAndDropService*>(pvUser);
+    DragAndDropService *pThis = static_cast<DragAndDropService*>(pvUser);
     DnDEvent e;
     do
@@ -1782,20 +1787,21 @@
          * events and if there are not any new one, sleep for a certain amount
          * of time. */
-        if (XEventsQueued(pSrv->m_pDisplay, QueuedAfterFlush) > 0)
+        if (XEventsQueued(pThis->m_pDisplay, QueuedAfterFlush) > 0)
         {
             RT_ZERO(e);
             e.type = DnDEvent::X11_Type;
-            XNextEvent(pSrv->m_pDisplay, &e.x11);
+            XNextEvent(pThis->m_pDisplay, &e.x11);
+#if 0
+            /* We never detect the stop event here for some reason */
             /* Check for a stop message. */
-//            if (   e.x11.type == ClientMessage
-//                && e.x11.xclient.message_type == xAtom(XA_dndstop))
-//            {
-//                break;
-//            }
-//            if (isDnDRespondEvent(pSrv->m_pDisplay, &e.x11, 0))
+            if (   e.x11.type == ClientMessage
+                && e.x11.xclient.message_type == xAtom(XA_dndstop))
+                break;
+#endif
+//            if (isDnDRespondEvent(pThis->m_pDisplay, &e.x11, 0))
             {
                 /* Appending makes a copy of the event structure. */
-                pSrv->m_eventQueue.append(e);
-                int rc = RTSemEventSignal(pSrv->m_hEventSem);
+                pThis->m_eventQueue.append(e);
+                int rc = RTSemEventSignal(pThis->m_hEventSem);
                 if (RT_FAILURE(rc))
                     return rc;
@@ -1804,5 +1810,5 @@
         else
             RTThreadSleep(25);
-    } while(!ASMAtomicReadBool(&pSrv->m_fSrvStopping));
+    } while (!ASMAtomicReadBool(&pThis->m_fSrvStopping));
 
     return VINF_SUCCESS;
