Help with thread implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Thornqvist

    Help with thread implementation

    Hi!

    I have a directory and file search class that I would like to call in an
    asynch manner. The class is implemented such that whenever a matching item
    is found (directory, file, file content) an appropriate event is triggered.
    The application/class using it subscribes to the events (to update UI or log
    to file or whatever) and the event args include a Cancel parameter that they
    can set to true to abort the search.

    I have a couple of questions:
    1. Where do I implement the threading? In the app/class calling the
    filesearcher class or in the filesearcher class directly?
    2. How do I make the events synchronuos? I need to wait for their completion
    since I need the Cancel parameter.
    3. Is there an alternative to using a Cancel parameter in the event args?
    4. If implementing threading in the filesearcher class is the best way to
    proceed, can I still maintain the synchronuos behavior already implemented?

    Coming from the Delphi world, I have had little problems getting to grips
    with .Net and C# (there are a lot of similarities), but .Net threading is
    one of those things that I can't quite get my head around so I am looking
    forward to your suggestions and ideas.

    --
    Regards, Peter


  • Peter Thornqvist

    #2
    Re: Help with thread implementation

    Oh, BTW I have read Jon Skeet's excellent article on threading
    (http://www.yoda.arachsys.com/csharp/threads/) but didn't really find a
    solution for my problem (or maybe I didn't understand it :))

    --
    Regards, Peter


    Comment

    • Peter Duniho

      #3
      Re: Help with thread implementation

      "Peter Thornqvist" <peter.tornqvis t@gmail.comwrot e in message
      news:urhI9kQAHH A.1224@TK2MSFTN GP04.phx.gbl...
      1. Where do I implement the threading? In the app/class calling the
      filesearcher class or in the filesearcher class directly?
      If you want the class to present an async interface to any client, then put
      the threading in the class itself. If not, then wrap the class with some
      threading functionality in the client.

      You can do it either way. Personally, I'd go ahead and build the threading
      into the class. That makes things a little cleaner and reusable. But
      there's no mandate that it be done one way or the other.
      2. How do I make the events synchronuos? I need to wait for their
      completion since I need the Cancel parameter.
      When your background thread calls an event handler, that happens on the
      background thread, synchronously with anything else that happens in the
      thread.

      There are other ways to run an event handler, but this is the way it happens
      in the simplest case. So your background thread would simply call the event
      handler normally, check the Cancel parameter on return and behave
      accordingly.

      Of course, this means that the handlers registered with your class should
      not take too much time to do anything. They should pretty much just note
      the information as desired, set the Cancel flag if necessary, and return as
      soon as possible. Otherwise, the entire background thread gets held up.
      3. Is there an alternative to using a Cancel parameter in the event args?
      Depends on what behavior you want. But sure, there's lots of other ways you
      could inform the background thread to stop searching, if that's what you
      want.
      4. If implementing threading in the filesearcher class is the best way to
      proceed, can I still maintain the synchronuos behavior already
      implemented?
      What synchronous behavior are you trying to maintain? You need not require
      the class client to use the async behavior if you don't want do. Making it
      optional should not be difficult, if that's what you mean.

      Pete


      Comment

      Working...