简体   繁体   中英

WPF WebBrowser locking UI even on separate thread using ObjectForScripting

Have a strange issue with the Webpage UI locking when calling a method on our ObjectForScripting that is bound to the WPF Web Browser Control. Further investigation has made us realize that the entire UI, including the WPF application and not just the WebBrowser, locks.

The technologies in use are WPF w/ Caliburn.Micro & Autofac. I use method on a COM visible class to push to the event aggregator that is provided by Caliburn.Micro. When this call is made the Web Page locks, even though we call the PublishOnBackgroundThread & the handler for the event immediately kicks off a new thread.

ObjectForScripting:

[ComVisible(true)]
public class ScriptBridge
{
     public void messageHost(string eventName, string eventPayload)
     {
         events.PublishOnBackgroundThread(new MessageFromUi(eventName, eventPayload));
     }
}

Handler

public void Handle(MessageFromUi message)
{
   Task.Factory.StartNew(() => { var result = ComObjectCall(); /* do more processing */ });
}

One thing i can mention is that within the action of the new thread, it does call another COM object. When I replace this COM call with a 10 second delay the UI does not lock.

Also, I have tried using Task.Run and the more explicit create Thread object.

Unfortunately I cannot call the COM object (within the handler) directly from from JavaScript.

Ok so I figured out the answer, turns out that I was initializing the COM object (the one that interacts with a device) on the UI thread and when I was calling a method in the COM Object it was running on the UI thread.

Moving the declaration of the COM Object to within the same thread helped.

As a side note, what also worked was creating the COM Object and a separate thread and then calling the method on a separate thread as well.

Thanks for anyone who looked at the questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM