none
Create multiple instances WPF app under one app process (help) RRS feed

  • Question

  • I need run multiple instances of WPF app under one application process. For example if native DLL run under one application multiple times, it is working without problem even if that DLL call c++/cli DLL with managed code. Problem is when my c++/cli code try run WPF dll application. When one instance is created, it is ok, but when I try load another instance, host application crash.

    In c++/cli I create STA thread and from that thread I create WPF application. As far as I know, this is only possible way how to WPF application run. 

    void WPF::wpf_app_interop::mf_AppCreate()
    {
    	p->app = gcnew WPF::WApp();
    	p->app->mf_INITIALIZE();
    	mf_WAIT();
    }
    
    // ------------------------------------
    
    void WPF::WApp::mf_INITIALIZE()
    {
    	LoggerCPPInt::mf_writeLog("C++/CLI", "Wapp()", "mf_INITIALIZE()");
    	Thread ^ th = gcnew Thread(gcnew ThreadStart(this, &WPF::WApp::mf_INITIALIZE_THREAD));
    	th->SetApartmentState(ApartmentState::STA);
    	th->Start();
    }
    
    void WPF::WApp::mf_INITIALIZE_THREAD()
    {
    	appInstance = gcnew App();
    	appInstance->mf_STARTUP();
    	mInstance = this;
    	appInstance->Run();
    }


    Another question, can I use static objects when multiple instancies are creted under host application?

    --------------------------------------------------------------------------------------------------

    Update

    I discover that WPF app can have only one instance per AppDomain. Now question is, how to create new AppDomain and use it from c++/cli? Create AppDomain is easy but what then? I tried load dll, but it still use main AppDomain and not new one.

    • Edited by Striebrovlas Friday, January 17, 2020 4:09 PM new question
    Friday, January 17, 2020 11:24 AM

Answers

  • Thanks for try help, unfortunately it doesn't help me.

    But I found solution myself. It was simple, but it is little complicated to do it and describe.

    Solution:

    I need a one c++ class which hold managed class and that managed class hold wpf application and have reference to c++ class to comunicate between (C# WPF and c++ plugin).

    The right setup is,

    1. first create thread with STA apartment and under that thread create new AppDomain and DoCallbackFunction where is created WPF application and setup pointers to call C# code from c++ and vice versa, as you needed.

    2. threads must be synchronized, for example you must wait when WPF application is created to continue in c++ code, I used AutoResetEvent for that. Keep in mind, everything from C# or WPF code is out of range  in c++/cli (when WPF is under another AppDomain) therefore AutoResetEvent must be used in managed class created in c++/cli project and during setup on second thread you pass delegate to use it in WPF.

    I am sorry, it is described very simply and lot of how to do is omitted, but I wrote the most important points.


    • Marked as answer by Striebrovlas Tuesday, January 21, 2020 6:21 PM
    • Edited by Striebrovlas Tuesday, January 21, 2020 6:30 PM add some tip
    Tuesday, January 21, 2020 6:20 PM

All replies

  • Hi Striebrovlas,

    Thank you for posting here.

    According to your description, I find two references:

    1. Prevent duplicate DLL from loading twice C++/CLI
    2. How do I force right AppDomain from native thread accessing managed code?

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 19, 2020 8:32 AM
    Moderator
  • Thanks for try help, unfortunately it doesn't help me.

    But I found solution myself. It was simple, but it is little complicated to do it and describe.

    Solution:

    I need a one c++ class which hold managed class and that managed class hold wpf application and have reference to c++ class to comunicate between (C# WPF and c++ plugin).

    The right setup is,

    1. first create thread with STA apartment and under that thread create new AppDomain and DoCallbackFunction where is created WPF application and setup pointers to call C# code from c++ and vice versa, as you needed.

    2. threads must be synchronized, for example you must wait when WPF application is created to continue in c++ code, I used AutoResetEvent for that. Keep in mind, everything from C# or WPF code is out of range  in c++/cli (when WPF is under another AppDomain) therefore AutoResetEvent must be used in managed class created in c++/cli project and during setup on second thread you pass delegate to use it in WPF.

    I am sorry, it is described very simply and lot of how to do is omitted, but I wrote the most important points.


    • Marked as answer by Striebrovlas Tuesday, January 21, 2020 6:21 PM
    • Edited by Striebrovlas Tuesday, January 21, 2020 6:30 PM add some tip
    Tuesday, January 21, 2020 6:20 PM