This is a few simple examples, but in the actual development, the delegation is only used in the reflection, and the event is only made once by itself, and it is specially used, in fact, it can not be used. Thread, because of the need, will use more points, here is mainly the thread on WS.
Entrust
In the previous reflection instance, it is also useful to delegate execution methods.
This example passes a method or delegate instance as an input parameter to another function.
As for what's the use, I haven't used it yet.
private delegate void Dosth(string s);//Define a string parameter with no return value of the delegate private void button2_Click_1(object sender, EventArgs e) { Dosth dosth = new Dosth(Func);//Instantiate delegate, DoMain(dosth);//DoMain(Func) Call function method DoMain,The input parameter can be the corresponding method Func It can also be delegated instantiated dosth } private void Func(string s) { MessageBox.Show(s); } private void DoMain(Dosth fun) //Method domainthe input parameter receives a delegate instance or function: the return value is null, and the input parameter is a string { fun("hello world!"); }
Event
Events use the publish subscribe model, events are declared and generated in the class, and are associated with event handlers by using delegates in the same class or other classes.
The class containing the event is used to publish the event, which is called publisher, and other classes receiving the event are called subscribers.
The publisher describes how to call an event, that is, trigger an event. In the subscriber, it is actually the specific execution of the event, and what is to be done in the subscriber finally.
This is an example.
Publisher class
Publish the event, define the setting of the logic that triggers the execution of the event.
public class Publish { private int value; public delegate void MyDelegate(string s);//Affirming delegation public event MyDelegate MyEvent;//Declaration based delegation defined event public Publish() { value = 3; } public void CompairValue(int n) { if (n == value)//Meeting conditions { OnValueChanged(); } } protected void OnValueChanged() { if (MyEvent != null) { MyEvent("hello world!");//Trigger method execution in subscriber } } }
Subscriber class
When triggered, do what should be done.
public class Subscribe { public void ShowMessage(string s) { MessageBox.Show(s);//Event triggering. The subscriber performs specified operations. More operations can be completed in this class } }
Trigger
private void button3_Click(object sender, EventArgs e) { Publish pub = new Publish(); Subscribe sub = new Subscribe(); pub.MyEvent += sub.ShowMessage;//Or new Publish.MyDelegate(sub.ShowMessage); bind the method in the subscriber to the event, that is, register the event in the publisher pub.CompairValue(3);//When the input parameter is 3, the event is triggered and the method in the subscriber executes }
Thread
The thread here is the thread on WS
public void DoMain(){
static object ThreadLock = new object(); public static Thread thread; lock (ThreadLock) { try { Boolean ThreadIsExit = thread.IsAlive;//If an error is reported, the thread does not exist if (!ThreadIsExit) { thread.Start(); } } catch {try { thread = new Thread(new ThreadStart(Dosth));//Start a thread, callback function SYNstart thread.IsBackground = true;//Set thread as background thread thread.Start(); } catch (Exception ex) { } finally { } } finally { } }
}
public void Dosth(){
}