控制台应用程序仅在从Visual Studio运行时捕获事件

时间:2016-08-22 10:45:52

标签: c# multithreading visual-studio event-handling console-application

我目前正在测试第三方API,其中某些事件是从我尝试使用简单的Windows控制台应用程序捕获的远程服务器触发的。

只要按F5运行我的控制台应用程序,我的方案就会顺利运行。事件被捕获并实时显示。

所以我尝试部署应用程序并单独运行它。当然现在main()一旦命令用完就终止程序执行。我在How to keep a .NET console app running?What is the best way to keep a C# Console app running等问题中尝试了多种解决方案。

使用无限循环,等待用户输入或waitone()结束事件导致阻塞状态,无法再捕获事件。

所以我坦率地问,有没有办法运行我的控制台模拟它运行Visual Studio调试器的方式?

仅供参考,您可以在下面找到我的示例代码:

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!");
        }
    }

1 个答案:

答案 0 :(得分:2)

添加

Console.Read();

最后在main方法中。 它不允许关闭应用程序,直到用户按任意键,它也不会阻止事件处理程序。