you would be best using "events", have a look at the example code that microsoft provide, http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
C# How to notify a class (simple true/false) when a static method in another class has been invoked?
-
24-03-2022 - |
Question
Question:
How do I notify/update my winform when ping has ran(which would change my hypothetical send indicator)?
Scenario:
Say I have thread that is off sending pings to a computer. while the main thread is showing/in charge of the winform gui - completely separate classes.
- I have a WinForm that shows two little status indicators(Send & Receive)
- I have a class
B
that has a static methodPing()
- I have a thread spawn that continually runs
Ping()
No correct solution
OTHER TIPS
As most people here have suggested used events to do that.
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();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow