c# - How to create a separate STA thread for a WPF form that is inside of a dll and not a WPF application? -


i have dll contains wpf form. dll called wpf application, unfortunately can't touch it. problem there no main method in wpf form place

[stathread] static void main() {     //... } 

the exception thrown right on constructor of class. how can around sta problem in case. again, not wpf application, wpf form places inside of dll.

namespace visualscannertest {     /// <summary>     /// interaction logic visualscanner.xaml     /// </summary>     public partial class visualscanner :window,iperipheral<visualscanner.visualscannersettings>     {         private visualscannersettings _settings;         private readonly ieventmanager _eventmanager;           public visualscanner(ieventmanager eventmanager):base()         {             initializecomponent();             _eventmanager = eventmanager;         }           private void submitbutton_click(object sender, routedeventargs e)         {              string input = inputextbox.text;              inputextbox.text = string.empty;             if (!string.isnullorwhitespace(input))             {                 _eventmanager.getevent<peripheralevent>().publish(new peripheraleventdata {rawdata = input});             }          }           public void initialize(visualscannersettings settings)         {             _settings = settings;         }          public ienumerable<string> registerimpulses()         {             yield break;         }          public string type         {             { return gettype().fullname; }         }          public sealed class visualscannersettings:peripheralsettings         {             public string data { get; set; }            }     } } 

i 1st suggest turn dll wpf application has it's own thread need different way communicate - such using dll both applications reference or using merhod of rpc, e.g. tcp socket.

alternatively try expose in dll needs called on sta thread of consumer. application call it, e.g.:

createform(); 

for example.

though may not work - need setup few things make wpf tick not matter of using sta thread:

  1. set setsynchronizationcontext dispatchersynchronizationcontext

    synchronizationcontext.setsynchronizationcontext(new dispatchersynchronizationcontext(dispatcher.currentdispatcher));

  2. create system.windows.application()

  3. load , add xaml resources application.loadcomponent()`.

    var resources = system.windows.application.loadcomponent(new uri("/styles.xaml", urikind.relative)); resources.mergeddictionaries.add(resources);

  4. you need programmatically pump dispatcher:

    dispatcher.currentdispatcher.pushframe();


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -