我怎么等到一个控制台应用程序处于空闲状态? [英] How do I wait until a console application is idle?

查看:379
本文介绍了我怎么等到一个控制台应用程序处于空闲状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有启动,承载了一堆的服务(长期运行的启动)一个控制台应用程序,然后等待客户端调用了进去。我有一个启动该控制台应用程序,让客户电话集成测试。我如何等待控制台应用程序使得客户端调用之前完成它的启动?

I have a console application that starts up, hosts a bunch of services (long-running startup), and then waits for clients to call into it. I have integration tests that start this console application and make "client" calls. How do I wait for the console application to complete its startup before making the client calls?

我要避免做 Thread.sleep代码(INT) ,因为这是依赖于启动时间(这可能会改变),我浪费时间如果启动的更快

I want to avoid doing Thread.Sleep(int) because that's dependent on the startup time (which may change) and I waste time if the startup is faster.

Process.WaitForInputIdle 仅适用于带UI应用程序(我证实,它扔在这种情况下除外)。

Process.WaitForInputIdle works only on applications with a UI (and I confirmed that it does throw an exception in this case).

我接受似的,尴尬的解决方案有控制台应用程序写入一个临时文件,当它准备。

I'm open to awkward solutions like, have the console application write a temp file when it's ready.

推荐答案

一种选择是创建一个命名的EventWaitHandle。这将创建可以在整个过程中使用同步对象。然后,你有你的客户应用程序等待,直到事件被继续之前发出信号。一旦主控制台应用程序已完成启动它可以通知该事件。

One option would be to create a named EventWaitHandle. This creates a synchronization object that you can use across processes. Then you have your 'client' applications wait until the event is signalled before proceeding. Once the main console application has completed the startup it can signal the event.

http://msdn.microsoft.com/en-us/library/41acw8ct(VS.80).aspx

作为一个例子,你的服务器控制台应用程序可能有以下。这不是编译所以它只是一个起点:)

As an example, your "Server" console application might have the following. This is not compiled so it is just a starting point :)

using System.Threading;
static EventWaitHandle _startedEvent;
static void main()
{  
  _startedEvent = new EventWaitHandle(false, EventResetMode.ManualReset, @"Global\ConServerStarted");

  DoLongRunnningInitialization();

  // Signal the event so that all the waiting clients can proceed
  _startedEvent.Set();
}



然后客户会做这样的事情。

The clients would then be doing something like this

using System.Threading;
static void main()
{  
  EventWaitHandle startedEvent = new EventWaitHandle(false, EventResetMode.ManualReset, @"Global\ConServerStarted");

  // Wait for the event to be signaled, if it is already signalled then this will fall throught immediately.
  startedEvent.WaitOne();    

//  ... continue communicating with the server console app now ...
}

这篇关于我怎么等到一个控制台应用程序处于空闲状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆