Yes, your code locks when you use it in a Winforms project. The problem is that the Ping class does its best to raise the PingCompleted event in the same thread called SendAsync (). To do this, use the AsyncOperationManager.CreateOperation () method.
The problem is that it actually works in a Winforms application. He is trying to raise the event to the main thread. But this will not work, since you blocked the main thread by calling the countdown .Wait (). Ping cannot complete since the main thread locks. The main thread cannot be executed because ping does not complete. Dead End City.
It runs in console mode because it does not have a synchronization provider such as Winforms. The PingComplete event will be raised in the threadpool thread.
Blocking the user interface thread is fundamentally wrong. A quick fix is ββto run the workflow code. Beware that this ProbeCompleted event is also fired at this workstation. Use Control.BeginInvoke () to marshal it into the user interface thread. Or use BackgroundWorker.
private void Form1_Load(object sender, EventArgs e) { PortScanner ps = new PortScanner(); ps.ProbeCompleted += new PingProbeCompleted(ps_ProbeCompleted); ThreadPool.QueueUserWorkItem((w) => ps.run_ping_probe()); }
And don't forget to remove the extra call to Signal ().
source share