0

Good day!

I want to subscribe on event at console app. For example, i have some data provider wich get event OnDataChanged -when new data from database come in.

So, my program is such like this:

public static void Main()
{
dataProvider.OnDataChanged+=myevent_OnDataChanged;
}

static void myevent_OnDataChanged(object sender, DataChangeEventArgs e)
{
 Console.WriteLine("my event!");
}

So, if it be an WinForm app - i know than user should click button and my event will be changed.

But how to suscribe and get event in console app? I should write :

 public static void Main()
{
while(true)
    {
     dataProvider.OnDataChanged+=myevent_OnDataChanged;
    }
}

Or Console.ReadLine()?

Thank you!

4

1 回答 1

2

你将不得不无限地运行。在那之前钩住处理程序。

尝试这个:

dataProvider.OnDataChanged+=myevent_OnDataChanged;
while (true)
{
    input = Console.ReadKey(true);

    if ConsoleKeyInfo(input.Key == ConsoleKey.C && (input.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
    {
        break;
    }
}
于 2014-03-18T06:07:35.667 回答