后台线程比UI线程需要更多的时间

时间:2023-01-06 00:05:32

I have some code which interops with some COM dlls and ActiveX controls, and then fetches me some result. I am trying to run this code in the background in another thread.

我有一些代码与一些COM dll和ActiveX控件交互,然后获取一些结果。我正在尝试在另一个线程的后台运行这段代码。

But I have a problem, if I use UI thread to achieve this, the application gets blocked, but the time taken is about 5-6 seconds approximately for this operation.

但是我有一个问题,如果我使用UI线程来实现这个,应用程序会被阻塞,但是这个操作所花费的时间大约是5-6秒。

If I move this code into a background thread, the UI remains responsive but the time taken almost doubles to 10-11 seconds. There is nothing different which I am doing, but is there any specific reason why this takes more time.

如果我将这段代码移动到后台线程中,UI将保持响应,但所需的时间将增加一倍,达到10-11秒。我所做的并没有什么不同,但是有什么特别的原因可以解释为什么这需要更多的时间。

As of now not able to put any code. I tried increasing the thread priority too. It did not help.

到现在为止还不能放任何代码。我也尝试增加线程优先级。它没有帮助。

Thanks

谢谢

1 个解决方案

#1


0  

You should probably profile this to see when the execution of that background thread actually starts, and what it's actual time-consumption is - start to finish. There're a number of pretty decent profilers that can do this for you. Remember, when you create a separate thread, that doesn't mean that it necessarily fires up right at that instant. And something might be interrupting it (such as something higher in priority). Plus, when you executed it on your UI thread, it had your UI thread's priority: what priority are you setting the background-thread to? As DeveloperGuo suggests - you should probably instantiate the COM object on that background thread: if that object doesn't have to hang around, then it is generally cleaner and more efficient to make that thread have full responsibility for the COM objects and other resources that it uses, and just provide a higher-level abstract API for the rest of your program-code to use. These are just generic suggestions - can't get more specific without seeing code.

您可能应该对其进行概要分析,以查看后台线程何时真正开始执行,以及实际的时间消耗是什么——开始完成。有一些相当不错的剖析器可以为你做这些。请记住,当您创建一个单独的线程时,这并不意味着它一定会在那个瞬间启动。有些东西可能会打断它(比如优先级更高的东西)。另外,当您在UI线程上执行它时,它具有UI线程的优先级:您将后台线程设置为什么优先级?DeveloperGuo表明——你应该在后台线程实例化COM对象:如果这个对象没有挂,然后它通常是更清洁,更高效的线程已经全部责任它使用的COM对象和其他资源,并提供了一个更高级别的抽象API的使用你的程序代码。这些只是一般的建议——如果没有看到代码,就无法得到更具体的建议。

#1


0  

You should probably profile this to see when the execution of that background thread actually starts, and what it's actual time-consumption is - start to finish. There're a number of pretty decent profilers that can do this for you. Remember, when you create a separate thread, that doesn't mean that it necessarily fires up right at that instant. And something might be interrupting it (such as something higher in priority). Plus, when you executed it on your UI thread, it had your UI thread's priority: what priority are you setting the background-thread to? As DeveloperGuo suggests - you should probably instantiate the COM object on that background thread: if that object doesn't have to hang around, then it is generally cleaner and more efficient to make that thread have full responsibility for the COM objects and other resources that it uses, and just provide a higher-level abstract API for the rest of your program-code to use. These are just generic suggestions - can't get more specific without seeing code.

您可能应该对其进行概要分析,以查看后台线程何时真正开始执行,以及实际的时间消耗是什么——开始完成。有一些相当不错的剖析器可以为你做这些。请记住,当您创建一个单独的线程时,这并不意味着它一定会在那个瞬间启动。有些东西可能会打断它(比如优先级更高的东西)。另外,当您在UI线程上执行它时,它具有UI线程的优先级:您将后台线程设置为什么优先级?DeveloperGuo表明——你应该在后台线程实例化COM对象:如果这个对象没有挂,然后它通常是更清洁,更高效的线程已经全部责任它使用的COM对象和其他资源,并提供了一个更高级别的抽象API的使用你的程序代码。这些只是一般的建议——如果没有看到代码,就无法得到更具体的建议。