Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait until a console application is idle?

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?

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 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.

like image 818
Anthony Mastrean Avatar asked Apr 29 '10 19:04

Anthony Mastrean


People also ask

How do I stop the console app from closing?

Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console. Readline() or ReadKey() functions. Save this answer.

How do I keep the console app alive?

Keep Console Open With the Ctrl + F5 Shortcut in C# Our program runs in debug mode when we run our code by clicking the start button in the Visual Studio IDE. If we want to run our code just like it does on languages like C and C++ after compilation, we have to also compile our code with the Ctrl + F5 shortcut.


2 Answers

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 ...
}
like image 188
Chris Taylor Avatar answered Oct 10 '22 22:10

Chris Taylor


What about setting a mutex, and removing it once start up is done. Have the client app wait until it can grab the mutex before it starts doing things.

like image 43
Malfist Avatar answered Oct 10 '22 21:10

Malfist