Events in C# Advanced - Lesson2


Lesson 2: Passing data to a consumer 

In the previous lesson we created an event and consumed it.  In doing so you probably noticed that our code would have been a little better if we could have determined whether or not the file actually existed.  We could then provide more information to the user and make our code a bit more useful. 

In this example we will expand upon the previous code by adding a special class that we will pass to the consumer with a little bit of data.This special class will inherit the EventArgs class and add a Boolean property that will indicate whether or not the file exists.Then our windows form application (the consumer) will not only be able to indicate to the user that the status of the file has changed, but it will also be able to determine if it has been create or deleted.  

We create a class that inherits from the EventArgs class.The class we are creating is quite simple, it has only one public property and takes one argument in the constructor.The public property FileExist is set when the class is created.The FileWatchEventArgs class will be passed to the consumer when the event is raised.So any data we place into this class will be accessible by the consumer.In this example we will only pass the Boolean value FileExist to the consumer. 

/* Lesson 2: Add a class used to pass data to the event consumer. */
public class FileWatchEventArgs : EventArgs
{
private readonly bool _fileExist;
public FileWatchEventArgs( bool FileExist )
{
_fileExist = FileExist;
}
public bool FileExist
{
get { return _fileExist; }
}
}

Since you have now created your own custom EventArgs class,you need to update the rest of your code that uses the EventArgs class to now utilize your new FileWatchEventArgs class.We start by updating the delegate declaration. 

// Lesson 2: Update the delegate to pass your new class to the handler
public delegate void FileWatchEventHandler(object sender, FileWatchEventArgs e);

Inside the FileWatch class you need to update the virtual methods argument from EventArgs to your new FileWatchEventArgs class.

protected virtual void OnFileChange(FileWatchEventArgs e)

The last change you need to make is to the MonitorFile method in the FileWatch class.The MonitorFile method calls the OnFileChange method to raise the event.  When the event is raised we need to pass the new FileWatchEventArgs class to the OnFileChange method. 

OnFileChange( new FileWatchEventArgs( bCurrentStatus ) );  

In this example we passed our Boolean value bCurrentStatus to the FileWatchEventArgs class, thus storing the data in the new instance of the class. The consumer will have the option of using the data from the FileWatchEventArgs class if they wish. 

Now that we have completed the changes to the FileWatch class, we need to update the consumer.Our update will utilize the FileExist public property of the FileWatchEventArgs to add functionality to the windows form. 

Add a checkbox to the form with an ID of chkFileExists.  We will check the box if the file exists and uncheck the box is the file no longer exists.

private void OnFileChange( object Sender, FileWatchEventArgs e )
{
/* Lesson 2: Note that we are now accepting FileWatchEventArgs as a
* parameter to this function. We can now see if the file exists or not. */
chkFileExists.Checked = e.FileExist;
listBox.Items.Add( DateTime.Now.ToString() + ": file changed." );
}

Once you update the form you can create a file with the name test.txt,the checkbox should automatically check.  Delete or rename the file and the checkbox will automatically clear.


Similar Articles