在启动后更改线程(生产者)的参数 [英] Change parameters of thread (producer) after it is started

查看:65
本文介绍了在启动后更改线程(生产者)的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里,



我有一个实现了生产者消费者概念的win form应用程序(参见下面的应用程序架构)。我想在gui中更改参数时更改生产者B的参数。



我的应用程序架构:

MAIN THREAD( GUI - >包含2个线程队列,从线程接收事件以显示threadstate)





线程A
制片人A

|

|

V

线程B< ---- -------如何在启动后更改此线程的参数?

消费者B

生产者B

|

|

V

多个帖子

多个消费者B





最好的方法是什么?





best

delicious_cake

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

推荐答案

不仅可以动态更改线程参数(线程启动时),还可以通过使用简单的线程包装器有效地解决启动参数的问题:



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





这里的关键是通过隐含的this参数将包装线程的整个包装实例传递给线程构造函数通过传递非静态方法(实例方法)正文



无论你做什么,请记住如果您在不同的线程中读取和/或写入包装器中的任何数据(其中一个可能是包装线程,但这不是必需的),您需要使用 lock来保护共享内存 System.Threading.ReaderWriterLockSlim ,请参阅 http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx [ ^ ]。



-SA



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


这是Gera的另一个简洁的解决方案ld Gibson Jr

在C#中创建进程内异步服务 [ ^ ]



最好的问候

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

Best regards
Espen Harlinn


这篇关于在启动后更改线程(生产者)的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆