Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Framework: Are objects apartment bound?
    text
    copied!<p><strong>Given:</strong> Constructing an ADO Connection object from one thread and giving it to another thread is <strong><em>forbidden</em></strong>. The two threads are different apartments, and even though the first thread will <strong>never</strong> touch it again (not even maintain a reference to it!), it doesn't matter. </p> <p>That ADO Connection object was created by ThreadA, ThreadA is the only thread, ever, under any circumstances, at all, ever, that is allowed to use that Connection object, ever.</p> <p>Now replace "ADO Connection" with "ADO.NET Connection". Does the same rule apply?</p> <hr> <p>i know that most objects in the .NET framework are not thread-safe. For example, the <a href="http://msdn.microsoft.com/en-us/library/system.collections.dictionaryentry.aspx" rel="nofollow noreferrer">DictionaryEntry</a> structure in the SDK says: </p> <blockquote> <p><strong>Thread Safety</strong><br> Any public <strong>static</strong> members of this type are thread safe. Any instance members are not guaranteed to be thread safe.</p> </blockquote> <p>i understand that <strong>not thread-safe</strong> means that i have to synchronize access to the object if i am going to access it from different threads. That's all well and good, and i could ensure only one thread access the object at a time:</p> <pre><code>lock (myObject) { ... } </code></pre> <p>But there's something more than not being thread-safe. </p> <p>In COM, (some) objects are bound to the "<em>apartment</em>" that created it. Once the object has been constructed on one apartment, you are <strong><em>forbidden</em></strong> from accessing it from another apartment - no matter how much you protect that object from multiple simultaneous thread access.</p> <p>Does a similar concept exist in .NET? </p> <hr> <h2>More Information</h2> <p>i know you are forbidden from accessing Controls from threads other than the one that created it - even if you use it in a thread-safe manner. This is not documented on MSDN:</p> <blockquote> <p><strong>Thread Safety</strong></p> <p>Only the following members are thread safe: BeginInvoke, EndInvoke, Invoke, InvokeRequired, and CreateGraphics if the handle for the control has already been created. Calling CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls.</p> </blockquote> <p>There is no mention of Controls throwing exceptions when you create and use them from a single thread - when that thread is not the first thread that was created when the application started.</p> <p>But what about arbitrary objects? What about:</p> <pre><code>public class MyClass { int _number; public int Number { get { return _number; } set { _number = value; } } } MyClass myObject = new MyClass(); </code></pre> <p>As long as i synchronize access to myObject two threads are allowed to talk to it?</p> <hr> <p>The same goes for:</p> <pre><code>List&lt;Object&gt; sharedList = new List&lt;Object&gt;(); </code></pre> <p>Two threads can talk to the list, as long as they don't do it simultaneously, usually with:</p> <pre><code>lock (sharedList) { sharedList.Add(data); } </code></pre> <p>are two threads allowed to touch the same object?</p> <hr> <p>The same goes for:</p> <pre><code>IAsyncResult ar = BeginSetLabelToTheValueINeed(label1); ... EndSetLabelToTheValueINeed(ar); </code></pre> <hr> <p>The same goes for:</p> <pre><code>//Fetch image on connection that is an existing DB transaction public static Bitmap GetImageThumbnail(DbConnection conn, int imageID) { } </code></pre> <p>being converted into the asynchronous delegate pattern:</p> <pre><code>//Begin fetching an image on connection that is an existing DB transaction IAsyncResult ar = BeginGetImageThumbnuts(conn, imageID, callback, stateOjbect); ... //Finish fetching an image on connection that is an existing DB transaction Bitmap thumb = EndGetImageNumbthail(ar); </code></pre> <hr> <p>Rather than answering the question, people went off on a discussion about design patterns in ADO.NET. Please answer the question. Ignore the examples if they confuse and distract your squirrel brains.</p>
 

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