C # Delegate and event EventHandle and EventArgs,
I. background
Because C # delegation and events are not well understood, and there are very few handwritten delegation and events at work, they will gradually only add, delete, modify, and query. The basic knowledge of delegation and events is organized.
Ii. Case studies
The case references the case in Zhang Yi's blog and is handled.
Create a console project. The monitor root directory contains the text.txt file.
Create a simple class named FileWatch, which contains the event FileWatchevent. This class will check the directory in which the application is executed (the current
Directory, which is usually under the project name/bin/debug.pdf and contains the text.txt file. If a file is deleted or created, an event is triggered.
At the same time, a MonitorFile method is provided to continuously query the file.
There are comments in the code.
1 // define delegate 2 public delegate void FileWatchEventHandler (object sender, EventArgs e ); 3 /// <summary> 4 /// define FileWatch class 5 /// </summary> 6 public class FileWatch 7 {8 // define event 9 public event FileWatchEventHandler FileWatchevent; 10 11 /// <summary> 12 // used to detect the test.txt File status 13 /// </summary> 14 public void MonitorFile () 15 {16 bool isExist = false; 17 while (true) 18 {19 isExist = File. exists ("test.txt"); 20 MyEventAgrs arg = new MyEventAgrs (); 21 arg. State = isExist? "Yes": "no"; 22 23 if (FileWatchevent! = Null) 24 {25 FileWatchevent (this, arg); 26} 27 28 Thread. sleep (200); 29} 30} 31 32} 33 // <summary> 34 // MyEventAgrs, the user returns the File status 35 // </summary> 36 public class MyEventAgrs: EventArgs37 {38 private string _ state; 39 40 public string State41 {42 get {return this. _ state;} 43 set {_ state = value;} 44} 45}
The following is the call method:
1 class Program 2 {3 static void Main (string [] args) 4 {// instantiate 5 FileWatch fw = new FileWatch (); 6 7 // register event 8 fw. fileWatchevent + = new FileWatchEventHandler (OnFileChange); 9 10 // open another Thread to detect file 11 Thread t = new Thread (fw. monitorFile); 12 t. start (); 13 14 Thread. sleep (1000); 15 var filePath = AppDomain. currentDomain. baseDirectory; 16 filePath = filePath + "test.txt"; 17 var fileStream = File. create (filePath); 18 fileStream. close (); // pay attention to closing the stream; otherwise, the following deletion will report 19 20 21 Thread. sleep (1000); 22 File. delete (filePath); 23 24 Console. readKey (); 25} 26 27 private static void OnFileChange (object sender, EventArgs e) 28 {29 MyEventAgrs arg = e as MyEventAgrs; // 30 Console used to get the file status. writeLine (DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + arg. state); 31} 32}
Running result:
Summary:
1. Create a delegate:
Public delegate void delegate name (object sender, EventArgs e );
2. If the status is obtained, you need to create a class that inherits EventArgs.
3. Create an event:
Public event Delegate type event name;
4. register an event and add its handling method.
Partial references from: http://wayfarer.cnblogs.com/archive/2004/04/20/6712.html