标签云

微信群

扫码加入我们

WeChat QR Code

I have a slow method that I'd like to run in a separate thread.This method uses a call on a COM object created in the main thread of the app.Lets assume the code that is slow is this:var bwImg = image.GetBitonalImage(); // <- slow image is a COM created in main threadviewer.Document = bwImg; // <-- ATL control accepting the B/W COM image If that matters, I am doing this in a WPF application and 'viewer' is in a WinFormsHost.I assumed that doing this would make this asynchronous and not block the running of the application:var t = new Thread((ThreadStart)(() =>{var bwImg = image.GetBitonalImage(); // <- this is in separate threadDispatcher.Invoke((Action)(() =>{viewer.Document = bwImg; // <- this again on the main}));}));t.Start();It is ok for me that the images gets shown at some later time, and I do not care how much later.But it happens that the application is blocked in the same way.I did profiling and indeed most time is spent in GetBitonalImage, and almost none in 'viewer.Document = bwImg'.Removing the code altogether makes the app responsive, so it's not other code fault.There is other code later after this that uses the same image COM object (not the B/W result but the original).Is this the correct way to do this? Can the problem be because of the COM object method call?


Seems my case. Thanks

2019年04月18日33分12秒

So what you are actually saying is that the method call on the COM object will run anyway on the thread that created it, not bothering to see in which thread the invocation has been done? I have little experience with COM objects, but if this is the case then I have no way of speeding this up. BTW it works, but it does not speed up the app. :D

2019年04月19日33分12秒

MarinoŠimić: No, I think Michael said that. ;)He's right btw.

2019年04月18日33分12秒