Index: trunk/src/VBox/Additions/common/VBoxService/VBoxService-win.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxService-win.cpp	(revision 29761)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxService-win.cpp	(revision 29762)
@@ -31,4 +31,6 @@
 DWORD                 g_rcWinService = 0;
 SERVICE_STATUS_HANDLE g_hWinServiceStatus = NULL;
+/** The semaphore for the dummy Windows service. */
+static RTSEMEVENT     g_WindowsEvent = NIL_RTSEMEVENT;
 
 void WINAPI VBoxServiceWinMain (DWORD argc, LPTSTR *argv);
@@ -331,10 +333,5 @@
         VBoxServiceWinSetStatus (SERVICE_RUNNING, 0);
 
-        /*
-         * Check that at least one service is enabled.
-         */
-        unsigned iMain = VBoxServiceGetStartedServices();
-        rc = VBoxServiceStartServices(iMain); /* Start all the services. */
-
+        rc = VBoxServiceStartServices();
         if (RT_FAILURE(rc))
             VBoxServiceWinSetStatus (SERVICE_STOPPED, 0);
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp	(revision 29761)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp	(revision 29762)
@@ -35,4 +35,5 @@
 #include <iprt/initterm.h>
 #include <iprt/path.h>
+#include <iprt/semaphore.h>
 #include <iprt/string.h>
 #include <iprt/stream.h>
@@ -54,6 +55,10 @@
 /** The default service interval (the -i | --interval) option). */
 uint32_t g_DefaultInterval = 0;
-/** Shutdown the main thread. (later, for signals.) */
-bool volatile g_fShutdown;
+#ifdef RT_OS_WINDOWS
+/** Signal shutdown to the Windows service thread. */
+bool volatile g_WindowsServiceShutdown;
+/** Event the Windows service thread waits for shutdown. */
+RTSEMEVENT g_WindowsServiceEvent;
+#endif
 
 /**
@@ -276,15 +281,14 @@
 
 
-unsigned VBoxServiceGetStartedServices(void)
-{
-    unsigned iMain = ~0U;
+/**
+ * Check if at least one service should be started.
+ */
+static bool VBoxServiceCheckStartedServices(void)
+{
     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
         if (g_aServices[j].fEnabled)
-        {
-            iMain = j;
-            break;
-        }
-
-   return iMain; /* Return the index of the main service (must always come last!). */
+            return true;
+
+   return false;
 }
 
@@ -294,9 +298,6 @@
  *
  * @returns VBox status code, errors are fully bitched.
- *
- * @param   iMain           The index of the service that belongs to the main
- *                          thread. Pass ~0U if none does.
- */
-int VBoxServiceStartServices(unsigned iMain)
+ */
+int VBoxServiceStartServices(void)
 {
     int rc;
@@ -307,4 +308,5 @@
     VBoxServiceVerbose(2, "Initializing services ...\n");
     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
+    {
         if (g_aServices[j].fEnabled)
         {
@@ -324,4 +326,5 @@
             }
         }
+    }
 
     /*
@@ -332,6 +335,5 @@
     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     {
-        if (    !g_aServices[j].fEnabled
-            ||  j == iMain)
+        if (!g_aServices[j].fEnabled)
             continue;
 
@@ -354,16 +356,23 @@
         }
     }
-    if (   RT_SUCCESS(rc)
-        && iMain != ~0U)
-    {
-        /* The final service runs in the main thread. */
-        VBoxServiceVerbose(1, "Starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
-        rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
-        if (RT_SUCCESS(rc))
-            VBoxServiceVerbose(1, "Main service '%s' successfully stopped.\n", g_aServices[iMain].pDesc->pszName);
-        else /* Only complain if service returned an error. Otherwise the service is a one-timer. */
-            VBoxServiceError("Service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
-        g_aServices[iMain].pDesc->pfnTerm();
-    }
+
+#ifdef RT_OS_WINDOWS
+    if (RT_SUCCESS(rc))
+    {
+        /* Block the main thread. */
+        VBoxServiceVerbose(1, "Waiting in main thread\n");
+        int rc = RTSemEventCreate(&g_WindowsServiceEvent);
+        AssertRC(rc);
+        for (;;)
+        {
+            if (g_WindowsServiceShutdown)
+                break;
+            rc = RTSemEventWait(g_WindowsServiceEvent, RT_INDEFINITE_WAIT);
+            AssertRC(rc);
+        }
+        RTSemEventDestroy(g_WindowsServiceEvent);
+        g_WindowsServiceEvent = NIL_RTSEMEVENT;
+    }
+#endif
     return rc;
 }
@@ -379,5 +388,4 @@
 {
     int rc = VINF_SUCCESS;
-    unsigned iMain = VBoxServiceGetStartedServices();
 
     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
@@ -390,31 +398,26 @@
         }
     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
-
-        if (    !g_aServices[j].fEnabled /* Only stop services which were started before. */
-            ||  j == iMain)              /* Don't call the termination function for main service yet. */
+    {
+        if (!g_aServices[j].fEnabled) /* Only stop services which were started before. */
+            continue;
+        if (g_aServices[j].Thread != NIL_RTTHREAD)
         {
-            continue;
+            VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
+            for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
+            {
+                rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
+                if (RT_SUCCESS(rc))
+                    break;
+#ifdef RT_OS_WINDOWS
+                /* Notify SCM that it takes a bit longer ... */
+                VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, i);
+#endif
+            }
+            if (RT_FAILURE(rc))
+                VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
         }
-        else
-        {
-            if (g_aServices[j].Thread != NIL_RTTHREAD)
-            {
-                VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
-                for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
-                {
-                    rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
-                    if (RT_SUCCESS(rc))
-                        break;
-#ifdef RT_OS_WINDOWS
-                    /* Notify SCM that it takes a bit longer ... */
-                    VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, i);
-#endif
-                }
-                if (RT_FAILURE(rc))
-                    VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
-            }
-            VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
-            g_aServices[j].pDesc->pfnTerm();
-        }
+        VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
+        g_aServices[j].pDesc->pfnTerm();
+    }
 
 #ifdef RT_OS_WINDOWS
@@ -425,10 +428,10 @@
      * function).
      */
-    if (iMain != ~0U)
-    {
-        VBoxServiceVerbose(3, "Stopping main service '%s' (%d) ...\n", g_aServices[iMain].pDesc->pszName, iMain);
-
-        ASMAtomicXchgBool(&g_fShutdown, true);
-        g_aServices[iMain].pDesc->pfnStop();
+    if (g_WindowsServiceEvent != NIL_RTSEMEVENT)
+    {
+        VBoxServiceVerbose(3, "Stopping the main thread...\n");
+        ASMAtomicXchgBool(&g_WindowsServiceShutdown, true);
+        rc = RTSemEventSignal(g_WindowsServiceEvent);
+        AssertRC(rc);
     }
 #endif
@@ -641,14 +644,6 @@
      * Check that at least one service is enabled.
      */
-    unsigned iMain = VBoxServiceGetStartedServices();
-    if (iMain == ~0U)
+    if (!VBoxServiceCheckStartedServices())
         return VBoxServiceSyntax("At least one service must be enabled.\n");
-
-#ifndef RT_OS_WINDOWS
-    /*
-     * POSIX: No main service thread.
-     */
-    iMain = ~0U;
-#endif
 
     VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
@@ -698,5 +693,5 @@
          *          and return immediately.
          */
-        rc = VBoxServiceStartServices(iMain);
+        rc = VBoxServiceStartServices();
 #ifndef RT_OS_WINDOWS
         if (RT_SUCCESS(rc))
@@ -720,3 +715,2 @@
     return RT_SUCCESS(rc) ? 0 : 1;
 }
-
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp	(revision 29761)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp	(revision 29762)
@@ -253,7 +253,4 @@
     bool                        fProcessTimedOut    = false;
     uint64_t                    MsProcessKilled     = UINT64_MAX;
-    bool const                  fHavePipes          = hStdInW    != NIL_RTPIPE
-                                                      || hStdOutR   != NIL_RTPIPE
-                                                      || hStdErrR   != NIL_RTPIPE;
     RTMSINTERVAL const          cMsPollBase         = hStdInW != NIL_RTPIPE
                                                       ? 100   /* need to poll for input */
Index: trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h
===================================================================
--- trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h	(revision 29761)
+++ trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h	(revision 29762)
@@ -261,6 +261,5 @@
 extern void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...);
 extern int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max);
-extern unsigned VBoxServiceGetStartedServices(void);
-extern int VBoxServiceStartServices(unsigned iMain);
+extern int VBoxServiceStartServices(void);
 extern int VBoxServiceStopServices(void);
 
