0

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!");
        }
    }
Community
  • 1
  • 1
lephleg
  • 1,724
  • 2
  • 21
  • 41
  • You could do something like fire a close event, or wait for a key to be hit with sleeps to stop blocking. How do you want to end it? – doctorlove Aug 22 '16 at 10:48
  • I've also tried to use an approach like the one you are referring, using `waitone()` for some closing event or flag but this also results in blocking the event capturing. As a further step I was aiming to deploy this as a windows service so it could run constantly in the background as soon as I start and stop the service. So for now just terminating the whole app to stop monitoring is good for me. – lephleg Aug 22 '16 at 10:57
  • 1
    Have you tried adding Console.Read(); at last in main method? May be it works. – Mehul Patel Aug 22 '16 at 11:15
  • @MD's wow.. it indeed worked! I really cant understand the difference between this and `ReadKey()` which seems to be blocking the events handling waiting for user input. However this makes me wonder it this would also work inside a windows service where there is no actual console.... – lephleg Aug 22 '16 at 12:16
  • @LePhleg accept as anwer. – Mehul Patel Aug 22 '16 at 13:34

1 Answers1

2

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.

Mehul Patel
  • 1,084
  • 2
  • 12
  • 28