首页 > 代码库 > 回收应用程序池解决方法

回收应用程序池解决方法

 

   以下程序就是实现自动关闭并回收应用程序缓冲池。

App.config内容

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- 程序运行时隔(单位:毫秒) -->
    <add key ="TimeSpan" value ="http://www.mamicode.com/10000"/>
    <!-- 要监视的进程(不加.exe),默认是vsjitdebugger -->
    <add key ="AppName" value ="http://www.mamicode.com/zhaoPin.ClientServiceV2"/>
    <!-- 要回收的应用程序池名称 -->
    <add key ="Appool" value ="http://www.mamicode.com/zhaoPin.Rc"/>
  </appSettings>
</configuration>

 

using System;
using System.Diagnostics;
using System.Configuration;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            int i = 0;
            bool tag=false;
            Console.WriteLine("请不要关闭此窗口!!");
            string dd = System.Configuration.ConfigurationManager.AppSettings["TimeSpan"].ToString();
            string appname = System.Configuration.ConfigurationManager.AppSettings["AppName"].ToString();
            string appool = System.Configuration.ConfigurationManager.AppSettings["Appool"].ToString();
            while (true)
            {
                System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcesses();
                foreach (Process pi in p)
                    if (pi.ProcessName == appname)
                    {
                        tag=true;
                        pi.Kill();
                    }
                if (tag)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    i++;
                    string msg = RecycleAppol(appool);
                    Console.WriteLine("成功关闭" + i + "次!!");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(msg);
                    tag = false;
                }
                System.Threading.Thread.Sleep(Convert.ToInt32(dd));
            }
        }
        static string RecycleAppol(string appoolname)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cscript.exe";
            p.StartInfo.Arguments = "c:\\windows\\system32\\iisapp.vbs /a "+appoolname+" /r";
            p.StartInfo.CreateNoWindow = false;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            return p.StandardOutput.ReadToEnd();
        }

    }
}

回收应用程序池解决方法