public static bool SetPreshutdownTimeout(SafeHandle handle, ref structenums.SERVICE_STATUS myServiceStatus)
{
SafeNativeMethods.SERVICE_PRESHUTDOWN_INFO spi = new SafeNativeMethods.SERVICE_PRESHUTDOWN_INFO();
spi.dwPreshutdownTimeout = 3600 * 1000;
IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(spi));
if (lpInfo == IntPtr.Zero)
{
string errorMessage = LastError();
LibraryLogging.Error("Unable to allocate memory for service action, error: {0}", errorMessage);
EventLog.WriteEntry("pGina", String.Format("Unable to allocate memory for service action\nerror:{0}", errorMessage), EventLogEntryType.Warning);
return false;
}
else
{
Marshal.StructureToPtr(spi, lpInfo, false);
if (!SafeNativeMethods.ChangeServiceConfig2(handle, (int)SafeNativeMethods.INFO_LEVEL.SERVICE_CONFIG_PRESHUTDOWN_INFO, lpInfo))
{
string errorMessage = LastError();
LibraryLogging.Error("ChangeServiceConfig2 error: {0}", errorMessage);
EventLog.WriteEntry("pGina", String.Format("ChangeServiceConfig2\nThe service will be forced to stop during a shutdown within 3 minutes\nerror:{0}", errorMessage), EventLogEntryType.Warning);
return false;
}
Marshal.FreeHGlobal(lpInfo);
}
return true;
}