【问题标题】:Best way of determining a remote computer has booted and running确定远程计算机已启动并运行的最佳方法
【发布时间】:2010-07-19 21:37:30
【问题描述】:

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

我在想以下几点:

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

有不同/更好的方法吗?

标记

【问题讨论】:

    标签: c# ping


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      我遇到了您遇到的确切问题,我发现编写自定义服务是最有用的。 (我实际上需要知道无头计算机何时准备好远程桌面服务以接受连接,我编写的程序实际上会在准备登录时向 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);
              }
          }
      }
      

      【讨论】:

      • 只是好奇:在那种情况下,您为什么不直接查询 RDP 端口?如果您能收到答案,那就好了。
      • 所以我不需要经常点击连接来查看它何时准备就绪。当我听到提示音时,它提醒我去启动 mstsc 并连接。
      • 似乎开发服务可能是最好的方法,虽然我可能依赖共享文件夹,因为用户可能需要登录才能运行一些测试软件,除非它将在没有用户登录的情况下运行。我仍然需要查看测试软件是否会在没有用户登录的情况下运行。在任何一种情况下,我都可以让服务检查用户是否已登录,然后将信号发送回控制器 PC 以开始测试。我选择了 Scott 的回答,因为他很乐意为我的事业提供源代码。
      • @lordhog 需要注意的一点是,如果您的代码不会像我的代码那样立即自行终止。代码的主循环不应该在 OnStart 函数中。 OnStart 应该创建自己的线程并在这里完成工作。我将更新示例。
      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2013-01-10
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多