none
Easy multithreading RRS feed

  • Question

  • I am currently writing a bunch of components that need to connect to external services to get data and in turn return data to the calling application.

    During this process I want to log data, but I want the logging part of my code to start and finish on a different thread.

    It's basically just one line of code.  What is the fastest and best way of making this one piece of code run on a different thread?
    Monday, November 16, 2009 3:37 PM

Answers

All replies

  • Your question is unclear.  "running one piece of code" on different threads...what?  You must deal with objects, concurrency, and all the other stuff that comes with going to a multithreaded solution.  There's no magical way to ignore all these factors.  Starting a logger on one thread and ending on another thread is too vague.  Do you want to create a single logging object to be shared by X threads?  When does this happen?  Where does this happen?  Why does this happen? 

    As far as the one line of code...re-read the first 3 sentences.
    Good coding involves knowing one's logical limits and expanding them as necessary.
    Monday, November 16, 2009 3:43 PM
  • As Mackey points out, multi-threading is never "simple". If you don't understand it, you will eventually get into trouble.

    However, if all you're doing is writing to a file (which is synchronized by the driver), you can probably just use the ThreadPool. It can execute a function asyncronously in a simple call.

    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx
    • Marked as answer by TheLearner Monday, November 16, 2009 4:34 PM
    Monday, November 16, 2009 3:48 PM