한 이벤트 처리기에서 다른 이벤트 처리기로 일부 데이터를 전달하려고하는데 약간 붙어 있습니다. 다음 코드에는 두 가지 이벤트가 있습니다.
"Ping Completed"이벤트에서 타이머 경과 이벤트에서 하나의 변수에 액세스해야합니다. 어떻게 할 수 있습니까?
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
for (int i = 0; i < this.View.Rows.Count; i++)
{
this.IP = View.Rows[i].Cells[2].Value.ToString();
PingOptions Options = new PingOptions(10, true);
Ping thisPing = new Ping();
thisPing.SendAsync(IPAddress.Parse(IP), 100, new byte[0], Options);
thisPing.PingCompleted += new PingCompletedEventHandler(thisPing_PingCompleted);
}
}
void thisPing_PingCompleted(object sender, PingCompletedEventArgs e)
{
//i need to accsess the "int i" of the above loop Here
}
Ping에서 파생 된 다음 Pings 생성자에 인수를 전달할 수 있습니다. 그러면 이벤트의 동작을 자유롭게 변경할 수 있으며 필요에 따라 변경할 수 있습니다.
나는 당신이 시작할 수있는 간단한 수업을 만들었습니다.
class ExtendedPing : Ping
{
public delegate void ExtendedPing_Completed(object sender, PingCompletedEventArgs e, int identifier);
public event ExtendedPing_Completed On_ExtendedPing_Completed;
private int _i = 0;
public ExtendedPing(int i)
{
_i = i;
base.PingCompleted += ExtendedPing_PingCompleted;
}
void ExtendedPing_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (On_ExtendedPing_Completed != null)
{
On_ExtendedPing_Completed(sender, e, _i);
}
}
}
https://msdn.microsoft.com/en-gb/library/ms228387(v=VS.90).aspx 에서 상속에 대해 조금 더 읽어보세요 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다