Reputation: 117
I have a console application that listens for connections on a certain port using a TcpListener
. I start the TcpListener
but when I open my program the console immediately exists. How can I avoid it exiting while I am still doing asynchronous work?
Upvotes: 1
Views: 76
Reputation: 203829
You can use a ManualResetEvent
to keep your application up. Just create one, wait on it, and signal it in your asynchronous code when you want the application to close:
static void Main(string[] args)
{
ManualResetEvent signal = new ManualResetEvent(false);
//start asynchronous work
//call signal.Set(); to close the application
signal.WaitOne();
}
Upvotes: 2
Reputation: 23796
As the last statement in your console app, add:
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
Upvotes: 0