Overview
- Essentially, you can use it anywhere you are responding to events or work with continuous streams
- Handling UI Events (Mouse clicks, Touch gestures)
- Working with Asynchronous Web requests (Web services)
- Analysing sensor data from Manufacturing, Health care, etc. devices in real time
- Processing data from continuous data streams (Log files, Stock feeds, etc.)
Basic Example
Firstly, fire up Visual Studio 2010 and create a new project, in my case I created a windows form application.
Use your trusted package manager "Nuget" and install the RXX nuget package.
public class FileSystemObservable
{
private readonly FileSystemWatcher _fileSystemWatcher;
public IObservable<FileSystemEventArgs> CreatedFiles { get; private set; }
public FileSystemObservable(string
directory,string filter, bool includeSubdirectories)
{
_fileSystemWatcher = new FileSystemWatcher(directory, filter)
{
EnableRaisingEvents = true,
IncludeSubdirectories = includeSubdirectories,
};
CreatedFiles = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
(h => h.Invoke,
h => _fileSystemWatcher.Created += h,
h => _fileSystemWatcher.Created -= h)
.Select(x => x.EventArgs);
}
}
Below is how you would use the FileSystemObservable. Firstly as the code reads I create a new instance of the FileSystemObservable specifying that I want to monitor the entire C drive and sub directories for all file and directory changes.
var createdfilewatcher = new
FileSystemObservable(@"c:\", "*.*",
true).CreatedFiles
.Subscribe(x =>
{
MessageBox.Show(x.Name + " Created");
});
No comments:
Post a Comment