Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

A C# Tail
By Narendra Maharaj

Here is a little utility that allows you to "tail" several files in one handy place. It demostrates several often used programming techniques, some specific to C#.

MDI (Multiple Document Interface) Elementary TreeView And RichTextBox Controls Elementary Threading Event Handling Delegates Stream and File I/O

Download mtail.zip

Outline of program
There are two central pieces: One is the GUI consisting of MainFrame and ChildForm, The other is the monitor class (in monitor.cs). The MainFrame is a simple MDI control its File -> New Monitor menu generates an event that invokes the OnClick_New() method which launches a OpenFileDialog from which multiple files can be selected. A for loop is used to create a ChildForm class for each file, and control is passed to the ChildForm. Each ChildForm sets up an event handler for its RichTextBox TextChanged event. When it gets control it adds a node to the MainFrame TreeView with the name of the file then it instantiates a Monitor class. It then spawns a thread and passes thread control to the Monitor.DoMonitoring() method. This way the main thread can respond to events such as a New Monitor request etc. while the various threads will do the work of monitoring the files. In an effort to seperate the GUI from the work class Monitor, the ChildForm passes a delegate to the Monitor class which uses it writes data to the RichTextBox. The Monitor class DoMonitoring() method sets up a FileSystemWatcher class and an event handler for the OnChanged event. Before passing control to the Windows event manager the Monitor class does a read of the entire file and prints it to the RichTextBox. Finally the Foreground and Background colors of each ChildForm can be independently set from the Color menu.

This program hardly scratches the surface of C# and Microsoft .NET. But it does provide another example of several key points of this new technology.