我創建了Windows服務應用程序。它根據app.config文件中包含的應用程序設置運行。它將同時安裝在不同的位置(網絡,PC)。每個位置都需要在app.config文件中設置自己的參數。所以我不希望它在安裝後自動運行。通過這樣做,每個位置的用戶將能夠打開配置文件並對其進行更改。然後他們可以開始服務。但在此之後,服務將永遠運行。即使他們重新啓動窗口,它也會在Windows打開後自動運行。第一次手動啓動後自動運行windows服務
這是我的安裝程序類。安裝後它不會自動運行。那很好。但是,如果我手動運行它並重新啓動PC,當重新啓動完成時,它仍然等待手動啓動。我該怎麼辦?
public partial class MyServiceInstaller : System.Configuration.Install.Installer
{
ServiceInstaller _serviceInstaller = new ServiceInstaller();
ServiceProcessInstaller _processInstaller = new ServiceProcessInstaller();
string _serviceName = "MyService";
string _displayName = "My Service";
string _description = "My Service - Windows Service";
public MyServiceInstaller()
{
InitializeComponent();
this.BeforeInstall += new InstallEventHandler(MyServiceInstaller_BeforeInstall);
_processInstaller.Account = ServiceAccount.LocalSystem;
_serviceInstaller.StartType = ServiceStartMode.Automatic;
_serviceInstaller.Description = _description;
_serviceInstaller.ServiceName = _serviceName;
_serviceInstaller.DisplayName = _displayName;
Installers.Add(_serviceInstaller);
Installers.Add(_processInstaller);
}
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
ServiceController sc = new ServiceController(_serviceName);
if (sc.Status != ServiceControllerStatus.Running)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(10000);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
sc.Stop();
}
else
{
RestartService(10000);
}
}
private void RestartService(int timeoutMiliseconds)
{
ServiceController service = new ServiceController(_serviceName);
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMiliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
void MyServiceInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());
foreach (ServiceController s in services)
{
if (s.ServiceName == this._serviceInstaller.ServiceName)
{
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = _serviceName;
ServiceInstallerObj.Uninstall(null);
}
}
}
}
您的代碼似乎將啓動類型設置爲自動。你可以檢查事件日誌,看看你的服務是否試圖自動啓動,但失敗。如果您的服務依賴尚未開始的其他服務,則會發生這種情況。 – sgmoore
@sgmoore感謝您的評論。我意識到我的代碼依賴於其他一些代碼,在重啓之後需要等待一段時間才能執行。請將此作爲答案寫下,我會標記它。 –