Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
FileSystemWatcher
Message
 
À
14/11/2006 16:58:03
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB.NET 1.1
Divers
Thread ID:
01169662
Message ID:
01170134
Vues:
9
>I have been playing around with FileSystemWatcher, and had a question...
>
>I set my event for Created, and I created a file... worked as expected with no problems. However, what it performs the event handler method, does it do it in a new thread? I put a thread.sleep in my event handler to test, and I dropped a few text files into the watched directory, and the thread.sleep didn't seem to affect things, so it leads me to believe that it is starting a new thread for every event.
>
>If this is the case, is there a limit to how many threads can start? Let's say in my event handler I am performing an action that takes 30 seconds, and for some reason 100 files got created in those 30 seconds... will 30 threads get created?
>
>This may be a generic EventHandler question rather than a FileSystemWatcher question...

According to the docs (see the SynchronizingObject property) which is referenced in the On* methods which raise the events:

"When SynchronizingObject is a null reference (Nothing in Visual Basic), methods handling the Changed, Created, Deleted, and Renamed events are called on a thread from the system thread pool. For more information on system thread pools, see ThreadPool.

When the Changed, Created, Deleted, and Renamed events are handled by a visual Windows Forms component, such as a Button, accessing the component through the system thread pool might not work, or may result in an exception. Avoid this by setting SynchronizingObject to a Windows Forms component, which causes the methods that handle the Changed, Created, Deleted, and Renamed events to be called on the same thread on which the component was created."

You might want to download a cool tool named Reflector (do a Google search on it - it's the first on the list). You can load the .NET assemblies and take a look at the code yourself.

There is some default number of threads available, but it should create a new thread if necessary.

In your case I'd probably suggest using lock(), or, if your code is really going to take 30 seconds you may just want to do something like add an entry to a collection inside of your class and have another thread just iterate through them and process them. That way you're not tying up a bunch of resources (and creating a ton of new threads unnecessarily).
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform