Click here to Skip to main content
13,897,068 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi there,

i've got a win forms application with a producer consumer concept implemented (see application architecture below). i want to change the parameters of the producer B if a parameter is changed in the gui.

my application architecture:
MAIN THREAD (GUI --> contains 2 queues for the threads, receives events from the threads for displaying threadstate)


Thread A
producer A
|
|
V
Thread B <----------- how do i change parameters of this thread after start?
consumer B
producer B
|
|
V
multiple Threads
multiple consumers B


whats the best way to implement this?


best
delicious_cake
Posted
Updated 30-Apr-13 4:47am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Not just changing thread parameters on the fly (when the thread is started) but also passing parameters for start can be effectively solved by using a simple thread wrapper:

internal class ThreadWrapper {

    internal ThreadWrapper(/*..*/) {
        // pass whatever needed from outside:
        Thread = new System.Threading.Thread(Body); //don't tell my it is illegal!
    } //ThreadWrapper

    internal void Start() { this.Thread.Start(); }
    internal void Abort() { this.Thread.Abort(); }

    //and so on...

    internal int Parameter {
        get { lock(lockObject) return fParameter; }
        set { lock(locjObject) fParameter = value; }
    }

    void Body() {
        try {
            while (true) {
                int parameter;
                lock(locjObject) { parameter = fParameter ;}
                //use parameter
            }
        } catch (ThreadAbortExeption e) {
            //post-mortal action in case of Abort
        finally {
            //all other final processing (
    } //Body

    object lockObject = new object();
    int fParameter;

} //class ThreadWrapper


The key here is making the whole instance of the wrapper accessible by the wrapped thread via the implicit "this" parameter which was passed to thread constructor by passing it the non-static method (instance method) Body.

Whatever you do, remember that if you read and/or write any data from/to the wrapper in different threads (one of them could be a wrapped thread, but this is not required) you need to guard the shared memory with lock or System.Threading.ReaderWriterLockSlim, see http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx[^].

—SA
   
v3
Comments
Espen Harlinn 10-Jul-11 14:11pm
   
Nice reply, my 5
Sergey Alexandrovich Kryukov 10-Jul-11 14:58pm
   
Thank you, Espen.
--SA
thatraja 10-Jul-11 16:05pm
   
Nice answer, 5!
Sergey Alexandrovich Kryukov 10-Jul-11 17:08pm
   
Thank you, Raja. It also help to eliminate useless parametrized thread start and type case associated with it. As "this" is passed, nothing else is needed.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Here is another neat solution by Gerald Gibson Jr
Create in-process asynchronous services in C#[^]

Best regards
Espen Harlinn
   
Comments
Sergey Alexandrovich Kryukov 10-Jul-11 15:11pm
   
Yes, interesting article, my 5.
It may be just right for OP or maybe overkill, I don't know -- needs some study and more info about OP's needs.
--SA
Espen Harlinn 10-Jul-11 15:46pm
   
Thanks, Sergey, you've also got a few other pieces around the site that might apply :)
thatraja 10-Jul-11 16:07pm
   
Looks interesting one, bookmarked that article.
5!
Espen Harlinn 10-Jul-11 16:10pm
   
Thank you, thatraja :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100