IPアドレスを50回pingしますが、開始をクリックするとプログラムがフリーズします。forループ中にフォームがフリーズする
どうすればいいですか?
private void button1_Click(object sender, EventArgs e)
{
string IP = textBox1.Text;
string[] IPBlocks = IP.Split('.');
for (int x = 0; x < 50; x++)
{
System.Threading.Thread.Sleep(50);
int IPLastBlock = Int32.Parse(IPBlocks[3]) + (x+1);
IP = IPBlocks[0]+"."+ IPBlocks[1]+"."+ IPBlocks[2]+"."+ IPLastBlock;
bool pingStatus = PingHost(IP);
textBox2.Text += String.Format("{0} => {1} \r\n", IP, pingStatus);
}
}
をハングアップしませんスレッド処理が完了するまでバーを押します。 –
あなたのイベントハンドラでは、UIスレッドをブロックしてUIの更新を停止するような長いタスクは実行できません。処理を行うために新しいスレッドを作成する必要があります。 http://stackoverflow.com/questions/28194943/creating-and-starting-a-task-on-the-ui-threadおよびhttp://stackoverflow.com/questions/661561/how-to-update-theを参照してください。 -gui-from-another-thread-in-c –