Home > Net >  Small white WPF in c # write a PING why IP program running interface has been card dead
Small white WPF in c # write a PING why IP program running interface has been card dead

Time:10-06

Below is a code, a running start cycle was stuck interface, LISTBOX also does not show
The namespace network connection to check
{
///& lt; Summary>
///MainWindow. Xaml interaction logic
///& lt;/summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent();
}

Private void Ip1_TextChanged (object sender, TextChangedEventArgs e)
{

}

Private void Check1_Click (object sender, RoutedEventArgs e)
{

Int runtimes=0;
Try
{
Var F1=new Func (TryPing);
Var result=F1. The BeginInvoke (((string) Ip1. Text), null, null);
Bool result1=F1. EndInvoke (result);
While (result1)
{
Runtimes++;

List1. Items. The Add (" PING success number "+ runtimes. ToString ());

Thread.sleep (5000);

Var result2=F1. The BeginInvoke (((string) Ip1. Text), null, null);
Result1=F1. EndInvoke (result2);



}
Ipstatus1. Text="PING success number" + runtimes. ToString () + "t \ \ t" + DateTime. Now. The ToString () + "appear offline";
}
The catch (Exception ex)
{

MessageBox. Show (ex. Message);
}



}
Public bool TryPing IP (string)
{

Bool result=false;

Ping p=new Ping ();
PingOptions options=new PingOptions ();
Options. DontFragment=true;

//string data="https://bbs.csdn.net/topics/test";
//byte [] buffer=Encoding. The ASCII. GetBytes (data);
Int timeout=2000;
PingReply reply=p. end (IP, the timeout);
If (reply. Status==IPStatus. Success)
{
Result=true;
}
The else
{
result=false;
}

return result;
}

Private void ListBox_SelectionChanged (object sender, SelectionChangedEventArgs e)
{

}


}
}

CodePudding user response:

CodePudding user response:

Your while (result1) have blocked the UI thread interface, the interface will, of course, card is dead,

CodePudding user response:

A thread or use timer, sleep in the main thread will certainly card

CodePudding user response:

Above two eldest brother, is there a specific modification measures

CodePudding user response:

Put your ping code in the Task. Run (()=& gt; {

//ping code

});

CodePudding user response:

Through the code analysis found that:

1, you don't understand the asynchronous invocation way in disorderly use, EndInvoke if writing asynchronous, should write in the callback function, which is directly block waiting for the results, when in the callback function, general is an asynchronous method has been completed, the use of direct access to the results,
2, if you have no special requirements to the UI interface, you also write in WinForm, don't WPF, WPF is more suitable for data display, because you can use the MVVM
3, you don't know the use of WPF, WPF programming in the data for the driver to core, generally don't like WinForm written event-driven
4, the most important, you have not c # basic introduction to

In view of the above code, it is recommended that:
1, understand the Thread. The Sleep () method of the function, principle and the realization of the asynchronous
2, suggest to do better in the WinForm

CodePudding user response:

Whether can use the threadpool and then put them into the thread pool, ping function function into the background, the main function should also not card dead how to run?

CodePudding user response:

Understand the event-based asynchronous way, try to use the timer timing rather than while in this form,
  •  Tags:  
  • C#
  • Related