• GPS平台 作为服务启动


    using System;
    using System.Threading;
    using System.ServiceProcess;
    using System.Collections;
    using System.Configuration.Install;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections.Specialized;
    using System.IO;
    namespace WSGPSServices
    {
        public partial class myService : ServiceBase
        {
            #region 成员变量
            public string strCurrPath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName;
            public string servicesname = "WSGPSServices";//服务  
            public string Proceename = "WSGPSGateway.exe";// 进程  
            #endregion
            protected override void Dispose(bool disposeing)
            {
                if (disposeing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                    Register(servicesname, strCurrPath);
                }
            }
    
            #region 服务启动与暂停
    
            protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                this.TmCkServices.Enabled = true;
            }
    
            protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                this.TmCkServices.Enabled = false;
            }
    
            #endregion
    
            // 保存文件 
            public static bool SaveFile(string FileName, string Content)
            {
                bool result = false;
                if (File.Exists(FileName))
                {
                    Encoding code = Encoding.GetEncoding("gb2312");
                    using (StreamWriter writer = new StreamWriter(FileName, false, code))
                    {
                        writer.Write(Content);
                        writer.Dispose();
                    }
                }
                return result;
            }
    
    
            #region 注册卸载服务
            //注册服务  
            public void Register(string servicesname, string strServiceInstallPath)
            {
                IDictionary mySavedState = new Hashtable();
                try
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(servicesname);
                    //服务已经存在则卸载    
                    if (ServiceIsExisted(servicesname))
                    {
                        UnInstallService(servicesname, strCurrPath);
                    }
                    service.Refresh();
                    //注册服务    
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    mySavedState.Clear();
                    myAssemblyInstaller.Path = "WSGPSServices.exe";
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Install(mySavedState);
                    myAssemblyInstaller.Commit(mySavedState);
                    myAssemblyInstaller.Dispose();
                    service.Start();
                    ProcessStartInfo startinfo = new ProcessStartInfo("WSGPSGateway.exe");
                    startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startinfo.Arguments = Application.StartupPath + "\\WSGPSGateway.exe";
                    Process.Start(startinfo);
                    InitializeComponent();
                }
                catch (Exception ex)
                {
                    throw new Exception("注册服务时出错:" + ex.Message);
                }
            }
            //卸载服务  
            public void UnInstallService(string strServiceName, string strServiceInstallPath)
            {
                try
                {
                    if (ServiceIsExisted(servicesname))
                    {
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext = true;
                        myAssemblyInstaller.Path = "WSGPSServices.exe";
                        myAssemblyInstaller.Uninstall(null);
                        myAssemblyInstaller.Dispose();
                        KillProcess();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("卸载服务时出错:" + ex.Message);
                }
            }
            //卸载服务时结束进程  
            private void KillProcess()
            {
                System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程       
                try
                {
                    foreach (Process thisproc in Process.GetProcessesByName(Proceename))
                    {
                        if (!thisproc.CloseMainWindow())
                        {
                            thisproc.Kill();
                        }
                    }
                }
                catch (Exception Exc)
                {
                    throw Exc;
                }
            }
            #endregion
            #region 启动服务与应用程序
            //启动服务(启动存在的服务,30秒后启动失败报错)    
            public void StartService(string serviceName)
            {
                if (ServiceIsExisted(servicesname))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("WSGPSServices");
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                        for (int i = 0; i < 30; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                ProcessStartInfo startinfo = new ProcessStartInfo("WSGPSGateway.exe");
                                startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                                startinfo.Arguments = Application.StartupPath + "\\WSGPSGateway.exe";
                                Process.Start(startinfo);
                                break;
                            }
                            if (i == 29)
                            {
                                throw new Exception("服务WSGPS Services启动失败!");
                            }
                        }
                    }
                }
            }
            //判断服务是否存在    
            public bool ServiceIsExisted(string serviceName)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController s in services)
                {
                    if (s.ServiceName == servicesname)
                    {
                        return true;
                    }
                }
                return false;
            }
            #endregion
            #region Timer定时任务与构造方法
            //Timer构造方法  
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.TmCkServices = new System.Windows.Forms.Timer(this.components);
                // 
                // timer1
                // 
                this.TmCkServices.Enabled = true;
                this.TmCkServices.Interval = 180000;
    
            }
            //Timer定时检查任务  
            private void Timers_Tick(object sender, EventArgs e)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController s in services)
                {
                    if (s.ServiceName == servicesname)
                    {
                        return;//存在返回空  
                    }
                    else
                    {
                        Register(servicesname, strCurrPath);//无当前服务则重新注册服务  
                    }
                }
            }
            #endregion
        }
    }
    
  • 相关阅读:
    .net core上传
    C#/.NET整数的三种强制类型转换(int)、Convert.ToInt32()、int.Parse()的区别
    14、Silverlight 滤镜到 UWP 滤镜的移植(二)
    13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
    1、揭秘通用平台的 HttpClient (译)
    12、uwp 开发的零碎总结
    11、使用 WinAppDeployCmd 部署appx 包到 Windows10 Mobile上(更新)
    10、Windows10 上,在窗口左侧向右滑动打开 SplitView 的 Pane面板
    09、win32 转换为 store app
    08、通过自定义依赖属性,用 StateTrigger 修改全局主题样式
  • 原文地址:https://www.cnblogs.com/lelese7en/p/2074140.html
Copyright © 2020-2023  润新知