Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way to do it:</p> <pre><code>if (System.Threading.Thread.CurrentThread.ManagedThreadId == ThreadB.ManagedThreadId) ... </code></pre> <p>I don't know enough about .NET's <code>Thread</code> class implementation to know if the comparison above is equivalent to <code>Equals()</code> or not, but in absence of this knowledge, comparing the IDs is a safe bet.</p> <p>There may be a better (where better = easier, faster, etc.) way to accomplish what you're trying to do, depending on a few things like:</p> <ul> <li>what kind of app (ASP.NET, WinForms, console, etc.) are you building?</li> <li>why do you want to enforce I/O on only one thread?</li> <li>what kind of I/O is this? (e.g. writes to one file? network I/O constrained to one socket? etc.)</li> <li>what are your performance constraints relative to cost of locking, number of concurrent worker threads, etc?</li> <li>whether the "else" clause in your code needs to be blocking, fire-and-forget, or something more sophisticated</li> <li>how you want to deal with timeouts, deadlocks, etc.</li> </ul> <p>Adding this info to your question would be helpful, although if yours is a WinForms app and you're talking about user-facing GUI I/O, you can skip the other questions since the scenario is obvious. </p> <p>Keep in mind that <code>// call performIO so that it is called (invoked?) on ThreadB</code> implementation will vary depending on whether this is WinForms, ASP.NET, console, etc. </p> <p>If WinForms, check out <a href="http://www.codeproject.com/KB/cs/AOPInvokeRequired.aspx" rel="nofollow noreferrer">this CodeProject post</a> for a cool way to handle it. Also see MSDN for how this is usually handled using <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx" rel="nofollow noreferrer">InvokeRequired</a>. </p> <p>If Console or generalized server app (no GUI), you'll need to figure out how to let the main thread know that it has work waiting-- and you may want to consider an alternate implementation which has a I/O worker thread or thread pool which just sits around executing queued I/O requests that you queue to it. Or you might want to consider synchronizing your I/O requests (easier) instead of marshalling calls over to one thread (harder).</p> <p>If ASP.NET, you're probably implementing this in the wrong way. It's usually more effective to use ASP.NET async pages and/or to (per above) synchronize snchronizing to your I/O using <code>lock{}</code> or another synchronization method. </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.
    3. 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