I have a console application, and I want it to wait till some event is raised. But it executes the code and exits:
static void Main(string[] args)
{
var someObjectInstance = new SomeObject();
someObjectInstance.SomeEvent += SomeEventHandler;
}
static void SomeEventHandler()
{
//Some logic
}
I want to make my application behave like a Windows application where
Application.Run(new Form1());
is called and the message loop is run.
But I don’t need neither a message loop nor any form. So it looks like overhead. Is there a more light-weight way to achieve my goal?
First off, unless
SomeObjectis going to raise the event on a separate thread, this won’t work without some form of processing inSomeObject. If it’s designed that way, however, this is fairly straightforward.A very efficient way of handling this is to just wait on a WaitHandle: