多线程实现会阻塞主线程 [英] Multithreading implementation blocks main thread

查看:155
本文介绍了多线程实现会阻塞主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我来找你帮忙.我有一个使用ODBC查询数据仓库(Teradata)的应用程序,并且由于主查询(宏)确实需要花费几分钟才能完成,所以我决定实现多线程处理,以使主线程不会停顿. />
辅助线程设置为在后台以低于正常的优先级运行.由于结果将显示在弹出"对话框中,因此我在窗体的Load事件上执行所有修饰操作,该事件还启动了一个计时器,该计时器在一秒钟后启动辅助线程.

从理论上讲,我的对话框应该保持响应"状态,而辅助线程检索结果集.我怎么知道它停滞了?简单,我用来显示"活动的动画.gif会在线程到达DataAdapter.Fill()方法时冻结.

有任何想法吗?我应该发布关键代码行吗?还是您读过很多类似的文章,简直是小菜一碟? :cool:

谢谢您的宝贵时间.

Alex.

Hi everyone,

I come to you for some help. I have an application that uses ODBC to query our data warehouse (Teradata), and since the main query (macro) does take a few minutes to complete, I decided to implement multi-threading so that the main thread does not stalls.

The secondary thread is set to run in the background with below-normal priority. Since the results are to be displayed in a "pop-up" dialog, I perform all cosmetic actions at the Load event of the form which also starts a timer that after one second, starts the secondary thread.

In theory, my dialog should remain "responsive" while the secondary thread retrieves the result-set. How do I know it stalls? easy, the animated .gif I am using to "show" activity, freezes when the thread hits the DataAdapter.Fill() method.

Any ideas? should I post the key lines of code? or have you read similar stuff so many times that this is a piece of cake? :cool:

Thanks for your time.

Alex.

推荐答案

我可以想象的是,您已经将其设置为datagrid(或任何其他UI组件)的数据源,并且因为您正在更新数据,UI控件将暂停并等待完成.使用此方法,可以防止在执行更新(或任何其他操作)时进行更新.我认为您可以通过将其删除为UI组件的数据源来防止停顿.

另一种情况是,使用要在另一个线程中执行的方法构造一个线程,但是无论如何最终还是直接调用它:
What I can imagine is that you already set it to be the datasource of a datagrid (or any other UI component) and because you are updating the data, the UI control halts and waits for completion. With this method it prevents updates while executing an update (or any other action). I think you can prevent stalling by removing it as datasource for UI components.

Another scenario is that you construct a thread with the method you want to execute in another thread, but eventually call it directly anyway:
Dim t As Thread
t = New Thread(AddressOf BackgroundProcess)
BackgroundProcess


代替:


Instead of:

Dim t As Thread
t = New Thread(AddressOf BackgroundProcess)
t.Start()



否则,您确实可以发布一些代码以使发现问题更容易:-D

祝你好运!



Otherwise... you could indeed post some code to make it easier to spot the problem :-D

Good luck!


您好,尼布尔(E.F. Nijboer),

感谢您的见解.您所说的而不是:"几乎是我在问题前所执行的确切实现.今天下午晚些时候,在向其他程序员解释代码时,我意识到使多线程实现无效的原因.

我决定在此过程中的某个地方添加一个小的例程,以修改表单的文本,以告诉"用户正在执行该过程的哪一步(该公司的常见做法).当我添加它时,我对自己说...这将需要使用InvokeRequired属性和Invoke方法,这样我就不会收到讨厌的非法跨线程操作"消息...,以某种方式我有个很好的主意,就是将ODBC调用封装在其中,因此,它是在主线程中执行的,从而使应用程序无响应. :doh:

一旦我从调用脚本"中删除了ODBC调用,所有操作又恢复了正常,该应用程序再次像一个超级按钮一样工作. :cool:

周末愉快.
Hi E.F. Nijboer,

Thank you for your insight. What you remarked as "instead of:", is almost the exact implementation I had before the issue. Late this afternoon, while explaining the code to a fellow programmer, I realized what voided the multi-thread implementation.

Somewhere along the road, I decided to add a small routine to modify the form''s text in order to "tell" the user which step of the process was being performed (common practice at this company). As I added it, I said to myself ...this will require the use of the InvokeRequired property and the Invoke method, so that I won''t get the nasty "illegal cross-thread operation" message..., and somehow I had the great idea of encapsulating my ODBC call inside of it, therefore, it was being performed within the main thread making the app non-responsive. :doh:

Once I removed the ODBC call from the "Invoke script", it all came back to normal and the app once again works like a charm. :cool:

Have a great weekend.


这篇关于多线程实现会阻塞主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆