zoukankan      html  css  js  c++  java
  • 添加windows服务

    Code
    一、安装服务:
    private void InstallService(IDictionary stateSaver, string filepath)
            {
                
    try
                {
                    System.ServiceProcess.ServiceController service 
    = new System.ServiceProcess.ServiceController("ServiceName");
                    
    if(!ServiceIsExisted("ServiceName"))
                    {
                        
    //Install Service
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext 
    = true;
                        myAssemblyInstaller.Path 
    =filepath;
                        myAssemblyInstaller.Install(stateSaver);
                        myAssemblyInstaller.Commit(stateSaver);
                        myAssemblyInstaller.Dispose();
                        
    //--Start Service
                        service.Start();
                    }
                    
    else
                    {
                        
    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                        {
                            service.Start();
                        }
                    }
                }
                
    catch (Exception ex)
                {
                    
    throw new Exception("installServiceError\n" + ex.Message);
                }
            }

    二、卸载windows服务:
            
    private void UnInstallService(string filepath)
            {
                
    try
                {
                    
    if (ServiceIsExisted("ServiceName"))
                    {
                        
    //UnInstall Service
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext 
    = true;
                        myAssemblyInstaller.Path 
    = filepath;
                        myAssemblyInstaller.Uninstall(
    null);
                        myAssemblyInstaller.Dispose();
                    }
                }
                
    catch (Exception ex)
                {
                    
    throw new Exception("unInstallServiceError\n" + ex.Message);
                }
            }

    三、判断window服务是否存在:
            
    private bool ServiceIsExisted(string serviceName)
            {
                ServiceController[] services 
    = ServiceController.GetServices();
                
    foreach (ServiceController s in services)
                {
                    
    if (s.ServiceName == serviceName)
                    {
                        
    return true;
                    }
                }
                
    return false;
            }

    四、启动服务:
    private void StartService(string serviceName)
            {
                
    if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service 
    = new System.ServiceProcess.ServiceController(serviceName);
                    
    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                        
    for (int i = 0; i < 60; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(
    1000);
                            
    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                
    break;
                            }
                            
    if (i == 59)
                            {
                                
    throw new Exception(startServiceError.Replace("$s$", serviceName));
                            }
                        }
                    }
                }
            }

    五、停止服务:
            
    private void StopService(string serviceName)
            {
                
    if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service 
    = new System.ServiceProcess.ServiceController(serviceName);
                    
    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        service.Stop();
                        
    for (int i = 0; i < 60; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(
    1000);
                            
    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                            {
                                
    break;
                            }
                            
    if (i == 59)
                            {
                                
    throw new Exception(stopServiceError.Replace("$s$", serviceName));
                            }
                        }
                    }
                }
            }
  • 相关阅读:
    InnoDB 事务
    InnoDB 索引
    MySQL 8 事务管理、数据库维护、改善性能
    MySQL 7 存储过程、游标、触发器
    MySQL 6 插入数据(INSERT INTOVALUESSELECT FROM)、更新和删除数据(UPDATE SET WHEREDELETE)、创建和操纵表、视图
    MySQL 5 联结表、创建高级联结、组合查询、全文本搜索
    MySQL 4 数据处理函数、汇总数据、分组数据、子查询
    MySQL 3 通配符、正则、计算字段
    MySQL 2 SQL数据使用(检索、排序、过滤:SELECT/FROM/LIMIT/ORDER BY/DESC/WHERE/AND/OR/IN/NOT)
    JavaScript相关-深入理解函数2
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1603552.html
Copyright © 2011-2022 走看看