问题:
如何在ping运行时通知/更新我的winform(这会改变我的假设发送指示符)?
情景:
假设我有关于将ping发送到计算机的线程。主线程显示/负责winform gui - 完全独立的类。
B
,它有一个静态方法Ping()
Ping()
答案 0 :(得分:1)
你最好使用“事件”,看看微软提供的示例代码,http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
答案 1 :(得分:0)
由于这里的大多数人都建议使用事件来做到这一点。
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e) {
Sample sample=new Sample();
sample.SampleEvent+=sample_SampleEvent;
sample.SampleMethod();
}
private void sample_SampleEvent() {
Console.WriteLine("SampleMethod has been executed and the method on the WinForm has been notified about it.");
}
}
public class Sample {
public event SampleEventHandle SampleEvent;
protected virtual void OnSampleEvent() {
var handler=SampleEvent;
if(handler!=null)
handler();
}
public delegate void SampleEventHandle();
public void SampleMethod() {
OnSampleEvent();
}
}