c# - Is there any simple out of box infrastructure to execute action (event) in a main manager thread? -


i implement main (manager) thread starts multiple worker threads. worker threads generate events, , calling provided event handler. execute event handlers in main thread (serialized). (just control.invoke executes action in thread in control created)

i know involves message loop in main manager thread, , message queue in callbacks serialized, please not go details explaining this.

i not want reinvent wheel, , implement scratch. there in .net out of box, or light opensource implementation this? (like concurrent pattern implementations or similar)

please note: there no gui, n+1 threads.

thanks in advance

as alternative possible constructs in task parallel library, can achieve through autoresetevent. here rough idea should able refine this. main thread, after starting worker threads, wait on autoresetevent. when worker thread wants main thread something, can set autoresetevent instance , main thread continue.

following simple example.

(at class level)

const int maxthreads = 5; autoresetevent[] _waithandles = new autoresetevent[maxthreads]; // 1 each thread actionenum[] _callbackactions = new actionenum[maxthreads]; // 1 each thread object _callbackactionslock = new object(); 

(main thread)

for (int = 0; < maxthreads; i++) {     _waithandles[i] = new autoresetevent(false);  }  // start 5 worker threads passing each thread zero-based index...  // wait loop: (int = 0; < maxthreads; i++) {     bool result = _waithandles[i].waitone(500);     if (result)     {         performaction(i);     } }  private void performaction(int i) {     switch (_actions[i])     {         case actions.callbacka: ...             break;         case actions.callbackb: ...             break;             ...     } } 

worker threads can set , wait handles this

(worker thread)

// ... work lock(_callbackactionslock) // if no other thread use index of _callbackactions no need lock {     _callbackactions[currthreadindex] = actions.callbackb; } _waithandles[i].set(); ... 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -