0

응답 성있는 IP 라운드 트립 시간 목록을 컴파일하기 위해 datagridview의 모든 IP에 핑 (ping)하는 응용 프로그램이 있습니다. 단계를 완료하면 RoundtripTime을 다시 datagridview로 푸시합니다.Ping.SendAsync를 datagridview와 함께 사용하는 방법?

... 
     foreach (DataGridViewRow row in this.gvServersList.Rows) 
     { 
      this.current_row = row; 

      string ip = row.Cells["ipaddr_hide"].Value.ToString(); 

      ping = new Ping(); 

      ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); 

      ping.SendAsync(ip, 1000); 

      System.Threading.Thread.Sleep(5); 
     } 
    ... 

    private static void ping_PingCompleted(object sender, PingCompletedEventArgs e) 
    { 
     var reply = e.Reply; 
     DataGridViewRow row = this.current_row; //notice here 
     DataGridViewCell speed_cell = row.Cells["speed"]; 
     speed_cell.Value = reply.RoundtripTime; 
    } 

내가 현재의 행을 얻을 DataGridViewRow row = this.current_row;을 사용할 때하지만 난 그냥 오류 키워드 '이'정적 function.so에서 사용할 수 없습니다를 얻을 방법 DataGridView에 다시 값을 밀어?

감사합니다.

답변

0

KAJ 말했다 무엇을. 그러나 Ping 요청의 결과가 그리드의 IP 주소에 연결되어 있지 않기 때문에 결과가 섞일 수 있습니다. 어떤 호스트가 먼저 응답 할지를 알 수 없으며, ping이 5ms를 넘으면 콜백 사이에서 currentrow가 변경되기 때문에 어떤 일이 발생할 수 있습니다. 당신이해야 할 일은 콜백에 대한 datagridviewrow 참조를 보내는 것입니다.

ping.SendAsync(ip, 1000, row); 

그리고 콜백 :

DataGridViewRow row = e.UserState as DataGridViewRow; 

또한 확인이 요청하지 않았다 타임 아웃을 만들기 위해 reply.Status을 확인 할 수 있습니다 그렇게하기 위해,가 SendAsync의 오버로드를 사용합니다.

+0

감사합니다. 그것은 내 간단한 방법입니다! – jean

0

this은 현재 인스턴스를 나타냅니다. 정적 메소드는 인스턴스에 대한 것이 아니라 그 유형에 대한 것입니다. 따라서 this을 사용할 수 없습니다.

따라서 이벤트 처리기 선언에서 static 키워드를 제거해야합니다. 그런 다음 메서드는 인스턴스에 대해됩니다.

는 또한 데이터 그리드보기를 업데이트하기 전에 다시 UI 스레드에 코드를 마샬링해야 할 수도 있습니다 - 그럼 다음과 같은 코드 뭔가해야 할 것입니다 경우 :

delegate void UpdateGridThreadHandler(Reply reply); 

private void ping_PingCompleted(object sender, PingCompletedEventArgs e) 
{ 
    UpdateGridWithReply(e.Reply); 
} 

private void UpdateGridWithReply(Reply reply) 
{ 
    if (dataGridView1.InvokeRequired) 
    { 
     UpdateGridThreadHandler handler = UpdateGridWithReply; 
     dataGridView1.BeginInvoke(handler, table); 
    } 
    else 
    { 
     DataGridViewRow row = this.current_row; 
     DataGridViewCell speed_cell = row.Cells["speed"]; 
     speed_cell.Value = reply.RoundtripTime; 
    } 
} 
+0

감사합니다. 저는 C#의 초보자입니다. 위임을 사용하는 것이 유용한 예입니다. – jean

관련 문제