Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

CSharp question: Passing data to running thread

Options
  • 14-08-2008 7:12pm
    #1
    Registered Users Posts: 250 ✭✭


    Hi

    Creating a thread in CSHARP is pretty straight forward, and passing parameters when the thread is starting is easy enough.

    I am having trouble sending data to a running thread for processing. I cannot really find a good example. At the moment I create a thread for every data that needs to be processed, process it, and exit the thread. That works ok, but I am more thinking of having only a single running thread, with some form of queue inside it. When I need something processed, I'll pass it to the thread using a method, where it's placed in a "queue" (or say arraylist), and process if queue entry count is bigger than 0.

    Any help will be appreciated.


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    There is a pattern for this called the worker thread pattern. This is an example of this in java, but should be trivial enough to convert to c#. Basically a worker thread takes tasks off a queue as they arrive and then executes them in sequence. (Slightly over complicated example but the main parts of interest are the ConcreteQueue and retriever classes and also the RunnableTask interface)

    http://www.java2s.com/Code/Java/Design-Pattern/WorkerThreadPatterninJava.htm

    There are different versions of this pattern where a pool of worker threads can be used if needed.

    This example is based on the command pattern. Of course if it is more suitable you could put your different data objects directly on the queue for the worker thread, instead of executing different types of command objects as in this example.


Advertisement