Skip to content

Instantly share code, notes, and snippets.

@kthompson
Created November 12, 2011 19:41
Show Gist options
  • Save kthompson/1361002 to your computer and use it in GitHub Desktop.
Save kthompson/1361002 to your computer and use it in GitHub Desktop.
// Retry once if necessary
bool hasDrivers = false;
for (int i = 1; i <= 2; i++)
{
// Start the service and then we'll wait for drivers to load
using (ServiceController service = new ServiceController("dsvideoserver"))
{
try
{
// Wait for the service to fully start before checking the drivers
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(2));
}
catch
{
}
// If the service started then wait for the drivers to load
while (service.Status == ServiceControllerStatus.Running)
{
// Now wait a little bit for the drivers to load
hasDrivers = instance.HasDriversLoaded();
if (hasDrivers)
break;
Thread.Sleep(100);
}
try
{
// Stop the service
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
}
catch
{
}
try
{
// If the service isn't stopped yet then kill it
if (service.Status != ServiceControllerStatus.Stopped)
{
foreach (Process proc in Process.GetProcessesByName("DSVideoServer.exe"))
proc.Kill();
}
}
catch
{
}
// If we have the drivers now then don't retry
if (hasDrivers)
break;
messenger(Properties.Resources.LocalHardware_FailedLoadDriversRetry);
}
// If we couldn't load the drivers then fail here
if (!hasDrivers)
throw new InvalidOperationException(Properties.Resources.LocalHardware_FailedLoadDrivers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment