当前位置: 首页>>代码示例>>C#>>正文


C# ServiceControllerStatus.Equals方法代码示例

本文整理汇总了C#中ServiceControllerStatus.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceControllerStatus.Equals方法的具体用法?C# ServiceControllerStatus.Equals怎么用?C# ServiceControllerStatus.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceControllerStatus的用法示例。


在下文中一共展示了ServiceControllerStatus.Equals方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: setServiceStatus

        private void setServiceStatus(ServiceControllerStatus status)
        {
            if (serviceController.Status.Equals(status)) //already in the wanted status
            {
                OnProgress(processIdx + 1);
            }
            else
            {
                try
                {
                    TimeSpan timeoutSec = TimeSpan.FromSeconds(timeout);

                    bool tryAgain = false;
                    int tryCount = 0;

                    do
                    {
                        try
                        {
                            if (status.Equals(ServiceControllerStatus.Running))
                                serviceController.Start(); //TODO: support arguments
                            else
                                serviceController.Stop();
                        }
                        catch (InvalidOperationException e)
                        {
                            //in some cases, the service status might be changed although the exception is thrown
                            Thread.Sleep(TimeSpan.FromSeconds(5)); //wait a little bit time to try again

                            serviceController.Refresh();
                            if (serviceController.Status.Equals(status)) //equal
                                tryAgain = false;
                            else if (tryCount < TOTAL_TRY) //not equal, not reach total
                                tryAgain = true;
                            else //when not equal to status, AND reach total try
                                throw e;

                            tryCount++;
                        }
                    } while (tryAgain);

                    serviceController.WaitForStatus(status, timeoutSec);

                    Console.WriteLine(serviceController.Status.ToString());

                    //check the status to make sure it is started properly
                    if (serviceController.Status.Equals(status))
                    {
                        OnProgress(processIdx + 1);
                    }
                    else
                    {
                        throw new InvalidOperationException(serviceController.ServiceName + " cannot be changed to status " + status.ToString());
                    }
                }
                //TODO: there should be configuration for the app
                //when error occurs to a service, should it stop or continue to next service?
                catch (InvalidOperationException e)
                {
                    OnError(e);
                }
                catch (System.ServiceProcess.TimeoutException e)
                {
                    OnError(e);
                }
            }
        }
开发者ID:rocksoccer,项目名称:AppStarter,代码行数:67,代码来源:ServiceUtil.cs


注:本文中的ServiceControllerStatus.Equals方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。