多线程重新审视 [英] Multithread Revisted

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

问题描述

在主线程上,我有一个Windows窗体状态栏,我希望在长数据库进程运行时更新
更新。数据库进程在

单独的线程上运行:


Dim t As New Thread(AddressOf SomeDatabaseProcess)


t.Start()


数据库进程将在一个长例程中调用大约10次,每次开始一个新线程
。但问题是我需要线程

才能在代码执行继续之前完成。这个不在同一个线程上的
的主要原因是我需要更新状态栏

更新,但UI冻结了。我尝试使用t.Join()等待

线程完成,但它不允许释放UI。并且我试图设置一个全局变量以检测线程何时完成,但是线程刚刚在Do循环中运行直到变量设置为止,
代码

也冻结了用户界面。关于如何使这个工作的任何想法?

Derek

On the main thread I have a status bar in a Windows form that I want to
update when a long database process runs. The database process runs on a
separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?
Derek


推荐答案

Hello Derek,


UI变得无法响应的原因是长数据库操作

阻止消息泵。而不是分离出不需要的新线程,

只需在更新状态栏后立即按下消息泵。

(请参阅MSDN文档了解application.DoEvents)


-Boo
Hello Derek,

The reason the UI becomes unresponsive is that the long database operations
block the message pump. Instead of spinning off new threads, which are unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo

在主线程上,我有一个Windows格式的状态栏,我想要

在长数据库进程运行时更新。数据库进程

在一个单独的线程上运行:

Dim t As New Thread(AddressOf SomeDatabaseProcess)


t.Start()


数据库进程将在一个长的
例程中调用大约10次,每次都启动一个新线程。但问题是我需要在代码执行继续之前完成线程
。主要因为这不仅仅是在同一个线程上运行的原因是我需要更新

更新了darn状态栏,但UI冻结了。我尝试使用

t.Join()等待线程完成,但它没有

允许UI被释放。我尝试设置一个全局变量,以便在线程完成后检测
,但是线程之后的代码只是在一个Do循环中运行,直到变量被设置为止,还冻结了

用户界面。关于如何使这个工作的任何想法?


Derek
On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek



7月4日星期二2006 17:29:29 -0700,Derek Hart写道:
On Tue, 4 Jul 2006 17:29:29 -0700, Derek Hart wrote:

数据库进程将在一个长例程中调用大约10次,

每次都开始一个新的主题。但问题是我需要线程

才能在代码执行继续之前完成。这个不在同一个线程上的
的主要原因是我需要更新状态栏

更新,但UI冻结了。我尝试使用t.Join()等待

线程完成,但它不允许释放UI。并且我试图设置一个全局变量以检测线程何时完成,但是线程刚刚在Do循环中运行直到变量设置为止,
代码

也冻结了用户界面。有关如何使其工作的任何想法?
The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?



在一个线程中执行所有数据库操作并使用Control.Invoke或

Control.BeginInvoke更新进度条。这些方法将为您提供对UI线程的调用。
。使用可追溯自VB6的Application.DoEvent循环比使用这些丑陋的

黑客更加干净。

Do all your database stuff in a single thread and use Control.Invoke or
Control.BeginInvoke to update your progress bar. These methods will
marshall the call to the UI thread for you. A lot cleaner than these ugly
hacks with Application.DoEvent loops dating from VB6.


Hello derek


所以你想要一个响应式进度条吧:-)


i使用以下


委托Sub AsynchMethodInvoker()

Public Sub SetPbValue(ByVal Count As Integer)

如果Me.pbMain.InvokeRequired那么

Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)

Me.Invoke(d,New Object(){Count})

Else

Me.pbMain.Value = Count

结束如果

结束子


现在只需从你的其他线程调用该方法,它就可以正常工作

: - )


ofcourse是pbmain的进度条

希望这有帮助

Michel Posseth [MCP]




" Mehdi"写道:
Hello derek

so you want a responsive progressbar huh :-)

i use the following

Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Me.Invoke(d, New Object() {Count})
Else
Me.pbMain.Value = Count
End If
End Sub

now just call the method from your other thread and it will just work fine
:-)

ofcourse is pbmain the progress bar
hope this helps
Michel Posseth [MCP]



"Mehdi" wrote:

On Tue,2006年7月4日17:29:29 -0700,Derek Hart写道:
On Tue, 4 Jul 2006 17:29:29 -0700, Derek Hart wrote:

数据库进程将在一个长例程中调用大约10次,

每次启动一个新线程。但问题是我需要线程

才能在代码执行继续之前完成。这个不在同一个线程上的
的主要原因是我需要更新状态栏

更新,但UI冻结了。我尝试使用t.Join()等待

线程完成,但它不允许释放UI。并且我试图设置一个全局变量以检测线程何时完成,但是线程刚刚在Do循环中运行直到变量设置为止,
代码

也冻结了用户界面。有关如何使其工作的任何想法?
The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?



在一个线程中执行所有数据库操作并使用Control.Invoke或

Control.BeginInvoke更新进度条。这些方法将为您提供对UI线程的调用。
。使用VB6中的Application.DoEvent循环比使用这些丑陋的

黑客更加干净。


Do all your database stuff in a single thread and use Control.Invoke or
Control.BeginInvoke to update your progress bar. These methods will
marshall the call to the UI thread for you. A lot cleaner than these ugly
hacks with Application.DoEvent loops dating from VB6.


这篇关于多线程重新审视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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