0

I have a program that should keep watching events that come from an outside source (FileSystemWatcher), and the source needs my handling code to be fast, or I could lose the next events.

But at anytime my program might be executing a heavy operation.

First question: 

If an outside event is raised during a heavy operation, will my program finish the operation before handling the event?

Second question:

I'm a newbie to Threading, so my main question is how can I create a separate permanent thread to do only the event handlings from the outside source? My first approach is that this thread will simply store the data to be accessed by the main code when it's ready for that, and use the heavy coding to handle the stored data.

Thank you.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • Have you read about async and await? http://msdn.microsoft.com/en-us/library/hh191443.aspx – TheGeekYouNeed Feb 19 '14 at 23:06
  • 1
    If I understand your question correctly, then what you can do is when an event is raised, save all the context of the event, and post a work item to a thread pool, and have pool thread do the heavy lifting and have original thread return to external code very quickly. This assumes that context that you save doesn't get invalidated once thread is returned. – LB2 Feb 19 '14 at 23:08
  • Your formatting is very irritating. Please have a look at the options and use something sane. http://stackoverflow.com/editing-help - Thank you. (No, formatting as `code` what should be a **caption**, **bold**, *emphasized* or a numbered list is NOT sane.) – JensG Feb 19 '14 at 23:39

1 Answers1

1

The event will run on the thread that fires the event. If you application is doing an expensive operation and the event is kicked off from another thread then the two things are independent lest synchronization between the two threads.

If you are processing the events by poling them one activity will hold up the next.

Without knowing what events you are processing and how they are raised this is only the most generic of advice.

--- Edit

Yes

Does FileSystemWatcher create its own thread?

Community
  • 1
  • 1
rerun
  • 25,014
  • 6
  • 48
  • 78
  • It's a FileSystemWatcher that raises the events. You mean I don't need to worry because the events will be handled already in a separate thread (the FileSystemWatcher thread)??? – Daniel Möller Feb 19 '14 at 23:16