Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The usual simple method of doing thread synchronization on a common resource or code block is to use a mutex (or, in this case, a critical section). Use the lock statement:</p> <p><a href="http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx</a></p> <p>This article says to do locking on the "this" pointer, but that can be dangerous, as outside callers could also acquire the same lock, which could break your program. Do your locking on a private class variable.</p> <p>Here is some modification of your example code to incorporate locking/a critical section:</p> <pre><code>class SomeClass : ISomeInterface { protected object ExecuteCommand(SvcCmdType cmdType, params object[] inputParams) { lock(executeCommandLock) { SvcCommandEventArgs eventArgs = new SvcCommandEventArgs(cmdType, inputParams); OnProcessingAppCommand(this, eventArgs); return eventArgs.OutputParamsList; } } private Object executeCommandLock = new Object(); } </code></pre> <p><strong>Edit:</strong></p> <p>(paraphrasing from comments). You mentioned that you might want to process all calls to ExecuteCommand on a single thread, asynchronously. You may be able to accomplish this with the Dispatcher class:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx</a></p> <p>Get a reference to the dispatcher on one thread. Pass that reference to the other threads. When those threads want to call ExecuteCommand, they use dispatcher.BeginInvoke. Since they use BeginInvoke, all calls to ExecuteCommand will then operate asynchronously, and not block on that thread. However, each version of ExecuteCommand will be queued up, and run sequentially dispatcher thread.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload