- Improve start / stop control
- Use GetSelectedService exclusively
Modified: trunk/reactos/subsys/system/servman/control.c
Modified: trunk/reactos/subsys/system/servman/query.c
Modified: trunk/reactos/subsys/system/servman/start.c

Modified: trunk/reactos/subsys/system/servman/control.c
--- trunk/reactos/subsys/system/servman/control.c	2006-01-22 23:29:53 UTC (rev 20994)
+++ trunk/reactos/subsys/system/servman/control.c	2006-01-22 23:35:43 UTC (rev 20995)
@@ -23,6 +23,7 @@
     LVITEM item;
     DWORD BytesNeeded = 0;
     DWORD Loop = 5; //FIXME: testing value. needs better control
+    DWORD dwStartTickCount, dwOldCheckPoint;
 
     item.mask = LVIF_PARAM;
     item.iItem = GetSelectedItem();
@@ -72,15 +73,28 @@
         return FALSE;
     }
 
+    /* Save the tick count and initial checkpoint. */
+    dwStartTickCount = GetTickCount();
+    dwOldCheckPoint = ServiceStatus.dwCheckPoint;
+
     /* loop whilst service is not running */
     /* FIXME: needs more control adding. 'Loop' is temparary */
     while (ServiceStatus.dwCurrentState != Control || !Loop)
     {
+        DWORD dwWaitTime;
+
+        dwWaitTime = ServiceStatus.dwWaitHint / 10;
+
+        if( dwWaitTime < 500 )
+            dwWaitTime = 500;
+        else if ( dwWaitTime > 5000 )
+            dwWaitTime = 5000;
+
         /* increment the progress bar */
         SendMessage(hProgBar, PBM_STEPIT, 0, 0);
 
         /* wait before checking status */
-        Sleep(ServiceStatus.dwWaitHint / 8);
+        Sleep(dwWaitTime);
 
         /* check status again */
         if (! QueryServiceStatusEx(
@@ -93,7 +107,22 @@
             GetError(0);
             return FALSE;
         }
-        Loop--;
+        
+        if (ServiceStatus.dwCheckPoint > dwOldCheckPoint)
+        {
+            /* The service is making progress. increment the progress bar */
+            SendMessage(hProgBar, PBM_STEPIT, 0, 0);
+            dwStartTickCount = GetTickCount();
+            dwOldCheckPoint = ServiceStatus.dwCheckPoint;
+        }
+        else
+        {
+            if(GetTickCount() - dwStartTickCount > ServiceStatus.dwWaitHint)
+            {
+                /* No progress made within the wait hint */
+                break;
+            }
+        }
     }
 
     CloseServiceHandle(hSc);

Modified: trunk/reactos/subsys/system/servman/query.c
--- trunk/reactos/subsys/system/servman/query.c	2006-01-22 23:29:53 UTC (rev 20994)
+++ trunk/reactos/subsys/system/servman/query.c	2006-01-22 23:35:43 UTC (rev 20995)
@@ -96,15 +96,10 @@
     SC_HANDLE hSc = NULL;
     LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
     ENUM_SERVICE_STATUS_PROCESS *Service = NULL;
-    LVITEM item;
     DWORD BytesNeeded = 0;
 
-    item.mask = LVIF_PARAM;
-    item.iItem = GetSelectedItem();
-    SendMessage(hListView, LVM_GETITEM, 0, (LPARAM)&item);
-
     /* copy pointer to selected service */
-    Service = (ENUM_SERVICE_STATUS_PROCESS *)item.lParam;
+    Service = GetSelectedService();
 
     /* open handle to the SCM */
     hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);

Modified: trunk/reactos/subsys/system/servman/start.c
--- trunk/reactos/subsys/system/servman/start.c	2006-01-22 23:29:53 UTC (rev 20994)
+++ trunk/reactos/subsys/system/servman/start.c	2006-01-22 23:35:43 UTC (rev 20995)
@@ -19,17 +19,13 @@
     SC_HANDLE hSc;
     SERVICE_STATUS_PROCESS ServiceStatus;
     ENUM_SERVICE_STATUS_PROCESS *Service = NULL;
-    LVITEM item;
     DWORD BytesNeeded = 0;
     INT ArgCount = 0;
     DWORD Loop = 5; //FIXME: testing value. needs better control
+    DWORD dwStartTickCount, dwOldCheckPoint;
 
-    item.mask = LVIF_PARAM;
-    item.iItem = GetSelectedItem();
-    SendMessage(hListView, LVM_GETITEM, 0, (LPARAM)&item);
-
     /* copy pointer to selected service */
-    Service = (ENUM_SERVICE_STATUS_PROCESS *)item.lParam;
+    Service = GetSelectedService();
 
     /* set the progress bar range and step */
     hProgBar = GetDlgItem(hProgDlg, IDC_SERVCON_PROGRESS);
@@ -70,10 +66,23 @@
         return FALSE;
     }
 
+    /* Save the tick count and initial checkpoint. */
+    dwStartTickCount = GetTickCount();
+    dwOldCheckPoint = ServiceStatus.dwCheckPoint;
+
     /* loop whilst service is not running */
     /* FIXME: needs more control adding. 'Loop' is temparary */
     while (ServiceStatus.dwCurrentState != SERVICE_RUNNING || !Loop)
     {
+        DWORD dwWaitTime;
+
+        dwWaitTime = ServiceStatus.dwWaitHint / 10;
+
+        if( dwWaitTime < 500 )
+            dwWaitTime = 500;
+        else if ( dwWaitTime > 5000 )
+            dwWaitTime = 5000;
+
         /* increment the progress bar */
         SendMessage(hProgBar, PBM_STEPIT, 0, 0);
 
@@ -91,7 +100,22 @@
             GetError(0);
             return FALSE;
         }
-        Loop--;
+        
+        if (ServiceStatus.dwCheckPoint > dwOldCheckPoint)
+        {
+            /* The service is making progress. increment the progress bar */
+            SendMessage(hProgBar, PBM_STEPIT, 0, 0);
+            dwStartTickCount = GetTickCount();
+            dwOldCheckPoint = ServiceStatus.dwCheckPoint;
+        }
+        else
+        {
+            if(GetTickCount() - dwStartTickCount > ServiceStatus.dwWaitHint)
+            {
+                /* No progress made within the wait hint */
+                break;
+            }
+        }
     }
 
     CloseServiceHandle(hSc);