Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

CSharp question: Passing data to running thread

  • 14-08-2008 07:12PM
    #1
    Registered Users, Registered Users 2 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,093 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