When each of the above events fires, we are updating the main textbox ( txtStatus) with some text which describes the event. Private void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e) Next we must subscribe to its four events – the Changed, Created, Deleted, and Renamed events. We are starting the FileSystemWatcher by setting its EnableRaisingEvents property to true. MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Īs you can see the code is quite self explanatory. Also watch for changes within sub directoriesįileSystemWatcher.IncludeSubdirectories = true įileSystemWatcher.EnableRaisingEvents = true Private void btnMonitor_Click(object sender, EventArgs e)įileSystemWatcher.Path = () Next we’ll configure the FileSystemWatcher and start monitoring a folder in the Monitor button’s event handler. Then add a FileSystemWatcher component to your form from the Components Toolbox in Visual Studio. Let’s create an example which uses this component.Ĭreate a new Windows Forms Application project and design a form which looks similar to the one below: NET framework provides a component called the FileSystemWatcher which is designed to do exactly what its name suggests – watch the file system for changes. When the original file is changed it would trigger an event and you can update the copy in the other location with the updated original file. A reason why you might want to do this is for example if you want to keep two files in different locations in sync. It provides a ToArray() method which copies the items in the queue to the new array.In this article I am going to show you how to monitor a folder for changes. IsEmpty = coll.IsEmpty //Returns False Copy ConcurrentQueue to Array Below is the example.īool isEmpty = coll.IsEmpty //returns True IsEmpty property returns true, if ConcurrentQueue is empty otherwise returns false. Int count = coll.Count() //returns 4 IsEmpty Property It returns the total number of items currently exists in queue. Count all ItemsĬoncurrentQueue provides a Count method. We have to dequeue that item and add it again. First task is adding new items to the queue and second task is enumerating items.Ĭatch (AggregateException ex) // No exceptionĬoncurrentQueue does not provide any method to update an item. In the below example, we have taken two tasks. Note: Enumeration in ConcurrentQueue is thread-safe. To retrieve all items, we can use ConcurrentQueue in for each loop. In the next TryDequeue method, it returns "3" and "4" items. When we again call TryPeek method, it returns "3" item. When we again use dequeue, it returns "2" item and remove it from queue. We use dequeue, it removes "1" item from the list and gives it in out parameter. We call TryPeek method again and again, it returns "1" item. Both method works same, TryDequeue method removes an item from the list and TryPeek does not remove an item from the list. In the below example, I have used both TryPeek and TryDequeue methods. Difference between TryPeek and TryDequeue methods When we again use dequeue method, it returns the second item inserted in the queue that is "2". In the above example, when we dequeue an item first time it returns "1" item in the out parameter. IsSuccessful = coll.TryDequeue(out item) Below is the example.īool isSuccessful = coll.TryDequeue(out item) It returns the item into an out parameter. It also returns true if operation is successful other returns false. TryDequeue method tries to get an item from the ConcurrentQueue, but it removes item from the list. Below is the example.īool isSuccessful = coll.TryPeek(out item) Ĭonsole.WriteLine(isSuccessful) //Returns TrueĪs TryPeek does not remove an item from the queue, it returns the same item again an again. It returns true if operation is successful otherwise returns false. TryPeek method tries to get an item from the ConcurrentQueue without removing item from the list. Retrieve/Remove Single Itemįor retrieving items, we have two methods. Enqueue method does not have any other overloaded methods. It is only method available to add item to ConcurrentQueue. Enqueue takes the argument which data type matches with the type argument. It provides an Enqueue method to add any item at the end of the queue. Below is the example.ĬoncurrentQueue coll = new ConcurrentQueue(ints) Add Items in ConcurrentQueue In the second constructor we can pass any collection class which implements the IEnumerable interface. If we want to copy existing collection into our ConcurrentQueue class then we have to use second constructor. Below is the example of creating an instance.ĬoncurrentQueue coll = new ConcurrentQueue() We can use this with any other data-type.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |