4

我正在编写一个控制另一台计算机的测试应用程序。通过 RS-232 端口(从使用 C# 应用程序运行 Windows XP SP2 的控制计算机)发送命令字符串来启动测试计算机,此时测试计算机将开机并启动到 Windows XP。我想知道确定该计算机何时完成启动过程并正常运行的最佳方法是什么。

我在想以下几点:

1)我正在考虑 ping 那台计算机,或者
2)有一个共享驱动器,如果能够访问该共享驱动器,或者
3)编写一个我可以与之通信的小型服务

有不同/更好的方法吗?

标记

4

2 回答 2

1

这一切都取决于您认为“已完成启动过程并正常运行”。例如,如果您只关心网卡初始化的那一刻,ping 可能会很好(只要 ECHO 端口没有关闭)。

共享不是一个好主意,因为它们通常仅在用户登录时才可用,这可能会或可能不会取决于您的情况。但即便如此,如果您更改配置或认为打开共享是安全漏洞怎么办?

如果您想确定地播放它,或者您只需要等到所有服务都启动,您应该考虑您的第三种选择。这是最容易做到的。让它监听 80 端口并从 IIS 运行。当被询问时,它可以回答机器的一些细节。这也将为您提供最大的灵活性。使用 IIS 可以帮助您不必编写自己的服务,并使安装和配置变得简单。

如果 IIS 不是一个选项,您当然可以考虑编写自己的服务。做起来并不难,但它需要你自己编写代码来监听某些端口。

于 2010-07-19T21:43:39.567 回答
0

我遇到了您遇到的确切问题,我发现编写自定义服务是最有用的。(我实际上需要知道无头机器何时准备好远程桌面服务以接受连接,我编写的程序实际上会在准备登录时向 PC 扬声器发出一点提示音。

编辑:如果您有兴趣,我会挖掘来源。

using System.ComponentModel;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading;

namespace Beeper
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
                { 
                    new Beeper() 
                };
            ServiceBase.Run(ServicesToRun);
        }
    }

    public partial class Beeper : ServiceBase
    {
        public Beeper()
        {
        }

        protected override void OnStart(string[] args)
        {
            if (MainThread != null)
                MainThread.Abort();
            MainThread = new Thread(new ThreadStart(MainLoop));
            MainThread.Start();
        }

        protected override void OnStop()
        {
            if (MainThread != null)
                MainThread.Abort();
        }

        protected void MainLoop()
        {
            try
            {
                //main code here
            }
            catch (ThreadAbortException)
            {
                //Do cleanup code here.
            }
        }

        System.Threading.Thread MainThread;
    }
    [RunInstaller(true)]
    public class BeeperInstaller : Installer
    {
        private ServiceProcessInstaller processInstaller;
        private ServiceInstaller serviceInstaller;
        public BeeperInstaller()
        {
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();
            processInstaller.Account = ServiceAccount.LocalSystem;
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "MyProgram";
            serviceInstaller.ServicesDependedOn = new string[] { "TermService" }; //Optional, this line makes sure the terminal services is up and running before it starts.
            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }
}
于 2010-07-19T21:46:41.890 回答