我正在开发Windows服务。它工作正常。当我试图从services.msc停止服务时,它会抛出以下错误:
Windows无法在本地计算机上停止xxx服务。该服务未返回错误。这可能是内部Windows错误或内部服务错误。如果问题仍然存在,请与系统管理员联系。 如果我再次尝试停止它,则需要花费大量时间然后抛出错误,如下所示:
Windows无法在本地计算机上停止xxx服务。错误1061:此时服务无法接受控制消息。
protected override void OnStop()
{
timer.Enabled = false;
EventViewerHelper.LogMessage(Resources.AppName, Resources.EventViewerServiceFinished, EventLogEntryType.Information);
}
protected override void OnStart(string[] args)
{
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
string configExchangeURL = Properties.Settings.Default.ExchangeURL;
if (args.Length > 0)
{
string UserName = args[0];
string Password = args[1];
string Domain = args[2];
service.Credentials = new WebCredentials(UserName, Password, Domain);
}
else
{
service.UseDefaultCredentials = true;
}
service.Url = new Uri(configExchangeURL);
service.TraceFlags = TraceFlags.None;
AppSettingsReader settings = new AppSettingsReader();
bool exchangeDownloadStreamSubscription = (bool)settings.GetValue("ExchangeDownloadStreamSubscription", typeof(bool));
if (exchangeDownloadStreamSubscription)
{
StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
var connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(streamingsubscription);
connection.OnNotificationEvent += connection_OnNotificationEvent;
connection.OnDisconnect += OnDisconnect;
connection.Open();
}
else
{
timer = new Timer();
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 120000;
timer.Enabled = true;
}
}
catch (Exception ex)
{
EventViewerHelper.LogMessage(Resources.AppName, string.Format(Resources.EventViewerServiceStartedError, ex.Message), EventLogEntryType.Error);
}
}