sc_xiaozhou 2017-04-06 01:33
浏览 715

C# 写windows服务 写好后 服务打开太慢

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace KillGames
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //string str = System.Environment.CurrentDirectory;
            ////存储进程的文件路径
            //string path = str + @"\ProcessList.txt";
            //string bat = str + @"\look.bat";
            string strPath = @"D:\KillGame\ProcessList.txt";
            FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            string[] ProcessNames = File.ReadAllLines(strPath);
            sw.WriteLine( DateTime.Now.ToString()+"\n");
            sw.Flush();
            sw.Close();
            fs.Close();
            KillProcess();

        }
        public static void KillProcess()
        {
            //string str = System.Windows.Forms.Application.StartupPath;
            //当前程序运行文件目录
            //存储进程名的文件
            string strPath = @"D:\KillGame\ProcessList.txt";
            string strBat = @"D:\KillGame\look.bat";
            //存储进程的文件路径
            //string path = str + @"\ProcessList.txt";
            //string bat = str + @"\look.bat";
            if (!File.Exists(strPath))
            {
                File.Create(strPath);
            }

            //逐行读取文件的进程名
            string[] ProcessNames = File.ReadAllLines(strPath);
            if (ProcessNames.Length > 0)
            {
                for (int i = 0; i < ProcessNames.Length; i++)
                {
                    //当前运行的所有进程
                    Process[] temp = Process.GetProcesses();
                    //rocessNames[i] = ProcessNames[i].Substring(0, ProcessNames[i].Length - 4);
                    foreach (Process vProcess in temp)
                    {
                        if (vProcess.ProcessName == ProcessNames[i])
                        {
                            vProcess.Kill();
                            //ProcessStartInfo psi = new ProcessStartInfo(strBat);
                            //Process p = new Process();
                            //p.StartInfo = psi;
                            //p.Start();
                            //Process.Start("cmd.exe");
                        }
                    }
                }
            }

            Thread.Sleep(10000);
            KillProcess();
        }
        protected override void OnStop()
        {
            FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(DateTime.Now.ToString() + "\n");
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    }
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配