Use an example to describe how to use the event.
Create a simple class named filewatch, which contains the onfilechange event. This class will check the running applicationProgramDirectory (current
Directory, which is usually under the project name/bin/debug.pdf and contains the file test.txt. 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.
Method:
Before creating an available event, declare a delegate and place it outside the class.
Public Delegate void filewatcheventhandler (Object sender, eventargs E );
Next, create a filewatch class. Then declare the event. Note that the event type is the delegate we previously defined.
Public event filewatcheventhandler filewatchevent;
Now create method onfilechange (). When this method is called, the event is triggered:
Protected virtual void onfilechange (eventargs E)
{
If (filewatchevent! = NULL)
{
Filewatchevent (this, e );
}
}
Finally, create method monitorfile (),
Public void monitorfile ()
{
Bool bcurrentstatus;
While (true)
{
Bcurrentstatus = file. exists ("test.txt ");
If (bcurrentstatus! = _ Blaststatus) // _ blaststatus indicates a private field and the initial value is false;
{
_ Blaststatus = bcurrentstatus;
Onfilechange (eventargs. Empty );
}
Thread. Sleep (250 );
}
}
CompleteCodeAs follows:
Using system;
Using system. Threading;
Using system. IO;
Namespace sample. Event
{
Public Delegate void filewatcheventhandler (Object sender, eventargs E );
Public class filewatch
{
Private bool _ blaststatus = false;
Public filewatch ()
{
//
// Todo: add the constructor logic here
//
}
Public event filewatcheventhandler filewatchevent;
protected virtual void onfilechange (eventargs e)
{< br> If (filewatchevent! = NULL)
{< br>
filewatchevent (this, e);
}< BR >}
Public void monitorfile ()
{
Bool bcurrentstatus;
While (true)
{
Bcurrentstatus = file. exists ("test.txt ");
// The status does not match. This indicates that the file is deleted or re-created. An event is triggered at this time;
If (bcurrentstatus! = _ Blaststatus)
{
_ Blaststatus = bcurrentstatus;
Onfilechange (eventargs. Empty );
}
Thread. Sleep (250 );
}
}
}
}
Use: Create a Windows application to test the events in filewatch we created earlier. Compile the project you just created
, Generate Assembly: filewatch. dll, and then add reference. Of course, you can add a project reference directly. Then in the Windows Application
Add a namespace in sequence:
Using sample. event;
Then define a private field in the application class. The type is the filewatch class we created earlier:
Public class frmmain: system. Windows. Forms. Form
{
Private sample. event. filewatch filewatcheventsource;
And instantiate the object in the constructor;
Public frmmain ()
{
Initializecomponent ();
Filewatcheventsource = new sample. event. filewatch ();
Connect the local method onfilechange to the event:
Filewatcheventsource. filewatchevent + = new sample. event. filewatcheventhandler (onfilechange );
We need to call the monitorfile method to trigger the event. In this example, we use a thread to control the monitorfile method. In this way
When the application is idle, run this method to trigger the event.
Thrd = new thread (New threadstart (filewatcheventsource. monitorfile ));
Thrd. Start ();
Finally, we need to know whether the event is triggered. to record the history of event triggering, we add a trigger in The ListBox control.
. Since the method called after the event is triggered is onfilechange, we put the operation in this method:
Private void onfilechange (Object sender, eventargs E)
{
ListBox. Items. Add (datetime. Now. tostring () + ": file changed .");
}
When an event is triggered, eventhanler will pass the reference of the sender and eventargs classes. The eventargs class is usually used for event sources and triggers.
Information is transmitted between devices. In this example, the eventargs class is neither transmitted nor used. Instead, add the event to ListBox.
.
The running result is as follows:
Conclusion: Key Points of using events in C:
First, you need to create a delegate in the format:
Public Delegate void delegate name (Object sender, eventargs E );
Note: The delegate is the function pointer in C. The parameter table is fixed because the event is to be transmitted and the information of the object that triggers the event in the event. The common format of the delegate name is: Name + envenhandle. In this way, the name is more standard.
Then, create an event field:
Public event Delegate type event name;
Note: The Event keyword indicates the event, and the return type is delegate;
Define another method to process the event, and then onfilechange (eventargs E) in this example ). In this method, you should call the event:
Event name (object, eventargs );
Here, the object is generally itself, the real parameter should be this, And the eventargs should be the real parameter passed in onfilechange, especially the value of the event to be passed.
Finally, create a method to trigger the event. In this example, monitorfile () is used. In this method, onfilechange is called when the conditions are met to trigger the event.
When using events, we usually need to define two methods. One is the same as the delegate signature defined by the event. In this example, It is onfilechange (Object sender, eventargs E );
Note that in the example, the onfilechange of the form class is different from the onfilechange of the event class. The latter is used to call events, and the former is used to bind events. The method to bind an event is simple. + = indicates adding an event, and-= indicates deleting an event.
In the example
Filewatcheventsource. filewatchevent + = new sample. event. filewatcheventhandler (onfilechange );
Add events.
In the example, the thread starts (THD. Start () and then calls the monitorfile () method. Event generation. After filewatchevent is generated, we bind the event filewatchevent to the onfilechange () method. Therefore, the onfilechange () method of the local window class is called to add information to The ListBox.