My doubt is that, in STA whether calls to different methods in the same interface is queued? or calls to the same method in same interface is queued?
In Single Thread Apartment model, is any call to a method is synchronised?
147 Views Asked by TuneFanta At
1
There are 1 best solutions below
Related Questions in COM
- How to program a COM object with an IEnumerator, IEnumerable interface inside
- WinAPI - right mouse drag & drop and IContextMenu
- Function Returning Excel COM Objects Unexpectedly
- Windows ContextMenuHandler names - Document conflict?
- How to publish a console application with COM interop and trim unused code
- IContextMenu Handler - Should ShellExecute or CreateProcess be used to InvokeCommand?
- Windows Explorer Conditional Context Menu Item for Drive?
- How do I fix an error while trying to send email using Outlook with COM?
- ITypeLib2.GetLibStatistics() always throws AccessViolationException in C#
- Directwrite is not always able to query font
- How can I pass a C++ struct to a C# DLL method using COM interop
- VBA PowerPoint Run-time error '-2147467259' (80004005): Presentation.Close: Failed
- PHP using a dll with COM + dotnet
- d3d11 triangle rendering failure despite everything being properly initialized
- COM context menu InvokeCommand not being called
Related Questions in APARTMENTS
- C# Not Detecting [STAThread] on main when clicking on DataGridViewComboBoxCell
- Using c++11 <thread> with COM threading apartments on Windows - hints, tips, pitfalls?
- What is a COM threading appartment?
- How to get the main thread of a console app to be MTA?
- In Single Thread Apartment model, is any call to a method is synchronised?
- Not able to handle com events in c#
- Error When Switching Tenant Using Apartment Gem
- Unable to set thread concurrency model to multithreaded apartment
- How can I set thread in PowerShell with executable scriptblock and ApartmentState argument?
- Error 'Unable to cast COM object of type' when crossing thread apartments in C#
- FreeThreadedDOMDocument, Neutral Apartments and Free-Threaded Marshaler
- To Marshal or Not to Marshal
- How can I set the COM apartment state for code loaded with `AppDomain.ExecuteAssembly`?
- Single-threaded apartment model - how to handle a COM object creating a worker thread?
- How to change apartment model in already existing Visual Studio C++ project?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
First the oversimplification:
Single-Threaded Apartments (STA) are constructs used to synchronize COM objects, not methods. As long as everybody plays by the rules, you are guaranteed that the object can only be accessed by one client at a time, that is, only one method can be called at a time. No method in the same object (by which of course I mean 'same instance', not 'of the same class') will be called randomly by a different thread while you are in the middle of a different call.
If you need to provide more granular synchronization, for example, only one method body needs to be synchronized, then apartments are not the right mechanism for you. The most flexible approach is to make your object free-threaded and manually code your own synchronization where needed. It is more work of course.
Now, I need to be more precise because we're both just hand-waving a lot of very important details:
The real goal of Single-Threaded Apartments is to provide thread-affinity for the object. The COM object's code can only run in the thread that created the object, and nobody else. If a different thread needs to talk to your object, they have to wait until the thread that owns your object is available. The fact that in a lot of scenarios that means method calling is queued is a natural side-effect of that.
"A lot of scenarios?"
Yes. Because apartments don't prevent reentrancy. Your method A() can call another method B() on the same object. And B() can call A() back. Or A() can call object2->MethodX() which itself calls your own method D() while you're still in the middle of executing A(). Or A() can trigger an Event (see Connection Points) and the event handler can call a different method E() on your object. So, referring to COM apartments as a synchronization mechanism for objects is a bit of a simplification that can get you in trouble if you don't think about the details.
Of course, mutexes and other synchronization primitives are also thread-bound, so they have the same reentrancy caveats. But by using vague language it might make you think that apartments do something they don't. Thinking that in an STA object only one method can be active at a time is a dangerous mental model.
Thread-affinity was a key goal of Single Threaded Apartments because COM was designed to be the modern foundation for OLE (the thing where you can drag a range of cells of a spreadsheet into a Microsoft Word document), in a multithreaded world. OLE objects rely a great deal on graphical system resources to paint their images, and those resources are thread-affine.