1

我正在使用 C# 程序来自动重启我的 DSL 路由器(如果它关闭)。到目前为止我写了这个:

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Net;

namespace RebootRouter
{

    static class Program
    {
        [STAThread]

        static void Main()
        {
            bool ProgramLoop = true;
            do
            {
                CheckAndRun();
                System.Threading.Thread.Sleep(180000);
            }
            while (ProgramLoop == true);
        }

        public static void CheckAndRun()
        {
            if (ScreenSaverCheck.IsScreenSaverRunning() == true)
            {
                if (InternetCheck.IsConnected() == false)
                {
                    string FirefoxBinary = (string)Registry
                                   .GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\    
                                   Microsoft\Windows\CurrentVersion\
                                   App Paths\firefox.exe", null, false);
                    Process LaunchMacro = new Process();
                    LaunchMacro.StartInfo.FileName = FirefoxBinary;
                    LaunchMacro.StartInfo.Arguments = "imacros://run/?
                                                   m=RebootRouter.iim";
                    LaunchMacro.Start();
                }
            }
        }

        public class ScreenSaverCheck
        {
            public static bool IsScreenSaverRunning()
            {
                Process[] FullProcessList = Process.GetProcesses();
                foreach (Process ProcessFromList in FullProcessList)
                {
                    if (ProcessFromList.ProcessName.EndsWith(".scr"))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        public class InternetCheck
        {
            public static bool IsConnected()
            {
                try
                {
                    IPAddress[] GoogleIPAddressList = Dns.
                                          GetHostAddresses("google.com");
                    if (GoogleIPAddressList[0].ToString().Length > 6)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                }
                return false;
            }
        }
    }
}

它按预期工作,但问题是该进程每次循环时都会将其内存使用量增加 15 KB。这是正常的还是有办法优化?任何帮助将不胜感激。

4

3 回答 3

1

我补充说:

public static void CheckAndRun()
{
    if (ScreenSaverCheck.IsScreenSaverRunning() == true)
    {
        if (InternetCheck.IsConnected() == false)
        {
            string FirefoxBinary = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe", null, false);
            using (Process LaunchMacro = new Process())
            {
                LaunchMacro.StartInfo.FileName = FirefoxBinary;
                LaunchMacro.StartInfo.Arguments = "imacros://run/?m=RebootRouter.iim";
                LaunchMacro.Start();
            }
        }
    }
}

和:

public class ScreenSaverCheck
{
    public static bool IsScreenSaverRunning()
    {
        Process[] FullProcessList = Process.GetProcesses();
        try
        {
            foreach (Process ProcessFromList in FullProcessList)
            {
                if (ProcessFromList.ProcessName.EndsWith(".scr"))
                {
                    return true;
                }
            }
        }
        finally
        {
            foreach (Process ProcessFromList in FullProcessList)
            {
                ProcessFromList.Dispose();
            }
        }
        return false;
    }
}

然后我测试了:

static void Main()
{
    int MyInteger = 0;
    do
    {
        CheckAndRun();
        MyInteger = MyInteger + 1;
    }
    while (MyInteger < 100000);
}

现在稳定了。内存不超过 5 MB。非常感谢 Douglas 和 Rob Walker。

于 2012-08-26T18:23:54.617 回答
0

首先,您应该注意处置其类实现IDisposable接口的所有对象——在您的情况下,Process.

例如:

using (Process LaunchMacro = new Process())
{
    LaunchMacro.StartInfo.FileName = FirefoxBinary;
    LaunchMacro.StartInfo.Arguments = "imacros://run/?m=RebootRouter.iim";
    LaunchMacro.Start();
}

处理数组时:

Process[] processes = Process.GetProcesses();
try
{
    foreach (Process p in processes)
        if (p.ProcessName.EndsWith(".scr"))
            return true;
}
finally
{
    foreach (Process p in processes)
        p.Dispose();
}
于 2012-08-26T17:06:10.757 回答
0

您应该在启动新进程之前终止现有的 firefox 进程。未连接并不自动意味着该进程不再运行。

于 2012-08-26T17:09:18.010 回答