none
Can the Application Context Be Obtained When in Another Thread? RRS feed

  • Question

  • I have a situation where I develop a dll that is loaded into a third-party application. For it to work with the third-party application, all I have to do is decorate a couple classes and methods and use it's API. There are two classes in particular regarding this question. One is used for commands and another implements an API interface allowing the dll to run a Terminate() and Initialize() method as the dll is being loaded or unloaded.

    I have found that my WCF service callback doesn't always work since it contains the third-party API code. I figured out with some benchtop testing that it's not getting the correct ExecutionContext for the callback, therefore it cannot see the API objects. 

    After some playing around I found that I can decorate a command with an attribute that allows it to run in the session state which means it's thread is the application context thread. The callback works when I do this, but this requires I have some mechanism for calling the command. The preference would be to do it in the other class in the Initialize() method. The problem here is that when the dll is loaded, the class is not instantiated in the same thread as the application context. Is there any way to do this? Maybe to be in one thread but obtain the application context thread and the executioncontext necessary for the service callback registration?

    I figure it should involve the SynchronizationContext, however none of these classes have access to any control with a SynchronizationContext to access... Any thoughts?


    Jason

    Wednesday, January 9, 2013 8:46 PM

Answers

  • I finally figured it out.  Turns out the third-party API has a little event Application.Idle that runs in the main Application Context thread.  So I used my Initialize() method to register the event, then in the event I unregister it (for a one-time use) and instantiate my class.  And auala! It works! 

    If anyone else using the same API runs into this, it's the AutoCAD API.


    Jason

    • Marked as answer by HuffCAD Thursday, January 10, 2013 10:28 PM
    Thursday, January 10, 2013 10:28 PM

All replies

  • You may be able to use a pipe (standard input and output) to comunicate between threads.

    jdweng

    • Marked as answer by HuffCAD Thursday, January 10, 2013 10:26 PM
    • Unmarked as answer by HuffCAD Thursday, January 10, 2013 10:26 PM
    Wednesday, January 9, 2013 9:55 PM
  • I finally figured it out.  Turns out the third-party API has a little event Application.Idle that runs in the main Application Context thread.  So I used my Initialize() method to register the event, then in the event I unregister it (for a one-time use) and instantiate my class.  And auala! It works! 

    If anyone else using the same API runs into this, it's the AutoCAD API.


    Jason

    • Marked as answer by HuffCAD Thursday, January 10, 2013 10:28 PM
    Thursday, January 10, 2013 10:28 PM