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:
set setsynchronizationcontext dispatchersynchronizationcontext
synchronizationcontext.setsynchronizationcontext(new dispatchersynchronizationcontext(dispatcher.currentdispatcher));
create
system.windows.application()
load , add xaml resources application.loadcomponent()`.
var resources = system.windows.application.loadcomponent(new uri("/styles.xaml", urikind.relative)); resources.mergeddictionaries.add(resources);
you need programmatically pump
dispatcher
:dispatcher.currentdispatcher.pushframe();
Comments
Post a Comment