locked
Should EnvDTE ItemOperations.OpenFile(filename) be called from another thread? RRS feed

  • Question

  • Hi,

    I am calling into EnvDTE.DTE from other threads. I have implemented the STA appartment and COM retry as detailed in this previous question. However, calling some functions such as dte.ItemOperations.OpenFile(filename) from this other thread causes a hang. Moving it back to the main UI thread makes it work.

    I have two questions:

    1. Is it safe to call OpenFile from the main UI control thread?

    2. How do I know which DTE functions it is safe to call from another thread?

    many thanks,

    Stewart.


    Saturday, August 30, 2014 10:50 AM

Answers

  • You can call from off-thread safely, but only if you know what state the UI thread is in when you make the call, which is pretty much impossible in an extensible app. When you call DTE off thread via a managed RCW the CLR causes it to go to the UI thread via an RPC call, if the UI thread is not accepting RPC you will wait, if the UI thread is waiting on your b/g thread you will deadlock.

    You didn't really give a lot of context for your question, what is the UI thread doing when you are making this b/g thread call? Has it invoked into your code and is waiting for it to finish and your code is waiting on this b/g thread, which is waiting to transition to the UI thread?
    Monday, September 1, 2014 5:21 PM

All replies

  • Speaking of my experience, nothing is guaranteed in terms of multi-threading in most Visual Studio methods because of it's deep extensible nature.

    What I mean is you never know what 3rd party package or addin your call will indirectly trigger. These packages may ot may not support multithreading. They may be written in any language that supports COM (that mean nearly all languages in fact...).

    Of course, the worst of it is, you never know what package or addin is installed (or will be installed) in a given Visual Studio installation.

    So my advice would be: since Visual Studio is a very hostile environment for extensions, don't assume anything.


    Simon Mourier



    • Edited by Simon Mourier Sunday, August 31, 2014 7:38 AM
    • Proposed as answer by Caillen Monday, September 1, 2014 5:55 AM
    Sunday, August 31, 2014 7:37 AM
  • I think I can see what is going on. VS must lock something internally when messages are processed by my controls, I assume this is so that I don't have to worry about EnvDTE threading issues from controls. It would be nice to have this confirmed though.

    My problem was that I was waiting in my control for an EnvDTE call to finish that was issued from one of my other threads. This seemed to cause a deadlock, which would make sense if VS was already locked.


    Sunday, August 31, 2014 8:16 AM
  • It is not recommended to call VS object model from another thread, since it does not guarantee thread-safe. Maybe you could try to debug the code to make sure it hang in a member of EnvDte.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 1, 2014 7:03 AM
  • You can call from off-thread safely, but only if you know what state the UI thread is in when you make the call, which is pretty much impossible in an extensible app. When you call DTE off thread via a managed RCW the CLR causes it to go to the UI thread via an RPC call, if the UI thread is not accepting RPC you will wait, if the UI thread is waiting on your b/g thread you will deadlock.

    You didn't really give a lot of context for your question, what is the UI thread doing when you are making this b/g thread call? Has it invoked into your code and is waiting for it to finish and your code is waiting on this b/g thread, which is waiting to transition to the UI thread?
    Monday, September 1, 2014 5:21 PM
  • Thank you for your answer. Yes, this is exactly what I was doing, I was waiting in the UI thread for a DTE call to finish on another thread, which caused a deadlock. Thank you for confirming how the RCW/RPC works.


    Monday, September 1, 2014 5:36 PM