Reputation: 1764
I'm currently testing a third-party API where some events are triggered from a remote server which I'm trying to capture with a simple windows console app.
My scenario runs smoothly as long as I run my console app pressing F5. Events are captured and displayed in real time as they should.
So I've tried to deploy the app and run it standalone. Of course now the main() terminates the program execution as soon as it runs out of commands. I've tried several solutions I've found in questions like How to keep a .NET console app running? or What is the best way to keep a C# Console app running.
Using an infinite loop, waiting for user input or waitone()
for a closing event resulted in the a blocking state where events are unable to be captured anymore.
So I'm frankly asking, is there a way to run my console emulating the way it runs with the Visual Studio debugger?
Just for reference you can find my sample code below:
class Program
{
static void Main(string[] args)
{
Program program = new Program();
//Login to third-part API service
Net2 net2 = new Net2();
net2.Login();
//Monitor events from a devoce
net2.client.MonitorAcu(1172079);
Console.WriteLine("Monitoring ACU #1172079. Listening for events...");
//Subscribe for events
net2.client.Net2AccessEvent += new OemClient.Net2AcuEventHandler(program.Net2AccessEvent);
}
private void Net2AccessEvent(object sender, IEventView e)
{
Console.WriteLine("An event was captured!");
}
}
Upvotes: 0
Views: 748
Reputation: 1194
Add
Console.Read();
at last in main method. It wont allow to close the application until user press any key and it wont block event handler also.
Upvotes: 2