تعتبر هذه الفئة مفيدة جداً في حالة رغبتنا في مراقبة سلوك ملف معين ومعرفة اي تغيير يطرأ عليها من التغييرات الموجودة في ال System.IO.NotifyFilters enum والذي يراقب اي من التغييرات التالية :
public enum NotifyFilters { Attributes, CreationTime, DirectoryName, FileName, LastAccess, LastWrite, Security, Size, } |
طبعاً بما اننا نتحدث عن events وتغييرات ، سنحتاج فوراً لتعريف دوال يتم تنفيذها مرتبطة بالاحداث ، اي دالة تستخدم لقراءة احداث مثل التعديل والتغيير وخلافه لا بد ان تكون على شكل delegate التالي :
void MyNotificationHandler(object source, FileSystemEventArgs e) |
حدث تغيير الاسم لا بد أن يتبع لل delegate التالي :
void MyNotificationHandler(object source, RenamedEventArgs e) |
الآن سنقوم بتعريف برنامج عادي ، يقوم بمراقبة حالة الملفات ، طبعاً ولإننا نرغب في ان نرى نتائج البرنامج فلا بد ان نجعل البرنامج يعمل حتى يضغط المستخدم حرف q مثلاً ، لو كنا في windows Forms كان بامكاننا المراقبة مباشرة طبعاً ، في مثالنا هذا سنراقب كافة الملفات النصية في القرص الصلب c:\ لحدثي الاضافة والحذف :
C#:
FileSystemWatcher watcher = new FileSystemWatcher();
// monitor files at:
watcher.Path = @"c:\";
// monitor files when
watcher.NotifyFilter = NotifyFilters.LastAccess NotifyFilters.LastWrite NotifyFilters.FileName NotifyFilters.DirectoryName;
// watch files of type
watcher.Filter = "*.txt";
// watch events:
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEventys = true;
Console.WriteLine("Press 'q' to quit app.");
while (Console.Read() != 'q') ; |
vb.net:
Dim watcher As New FileSystemWatcher()
' monitor files at:
watcher.Path = "c:\"
' monitor files when
watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
' watch files of type
watcher.Filter = "*.txt"
' watch events:
AddHandler watcher.Created, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnChanged
watcher.EnableRaisingEvents = True
Console.WriteLine("Press 'q' to quit app.")
While Console.Read() <> "q"C
End While | كما لاحظت ، عندما يحدث أي حدث فإننا نقوم باستدعاء الدالة OnChanged ، يمكن تخصيص دالة لكل حدث أو كما تحب لكن هذا فقط مثال ، يمكننا كتابة الدالة OnChanged بالشكل التالي مثلاً - عرض الملف والتغيير ووقت التغيير - :
C#:
static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File Changed, File Path: {0} , Change: {1}, DateTime: {2}", e.FullPath, e.ChangeType,DateTime.Now.ToString());
} | vb.net:
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
Console.WriteLine("File Changed, File Path: {0} , Change: {1}, DateTime: {2}", e.FullPath, e.ChangeType, DateTime.Now.ToString())
End Sub | والآن جرب البرنامج ...
الصورة التالية نتاج متوقع لحذف ملف من السي واعادته مرة أخرى ( من السي مباشرة ) :
التسميات: C# |
Using PLNQ queries
Managing deferred query execution
Controlling concurrency
Handling PLINQ Exceptions
Cancelling PLINQ queries
Setting merge options
Using custom partitioning
Using custom aggregation
Generating parallel ranges
Common problems and their causes
Testing & debugging
Making things better when everything goes wrong
Measuring parallel performance
Finding parallel bugs
Common parallel algorithms
Sorting, searching, and caching
Using parallel map and reductions
Speculative processing
Using producers and consumers