多线程循环冻结ui [英] Multi threading loop freezes ui

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

问题描述

我在应用程序中遇到mutli线程编程时遇到麻烦。
实际上,代码看起来像这样:

 私人委托Sub IniciarDiscagemDelegate()

Private Sub CallBack()
'做一些东西....
ThreadDiscagem =新的线程.Thread(AddressOf IniciarDiscagem)
ThreadDiscagem.IsBackground = True
ThreadDiscagem.Start()
End Sub

Private Sub IniciarDiscagem()
如果Me.InvokeRequired()然后
Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf IniciarDiscagem))
退出Sub
End If
Do While True
'此布尔值用于控制导航。当用户关闭当前条目时,我设置为false。
如果不是blnEntryAlreadyOpen然后LookForNewEntries()
循环
End Sub

Private Sub LookForNewEntries()
Dim blnFoundIt As Boolean = False
虽然不是blnFoundIt
blnFoundIt = DatabaseMethod()
循环
'一些会改变UI行为的代码,显示一些控件并隐藏别人。
End Sub

Private Sub DataBaseMethod()
'在DB上查找新条目的代码
return true
End Sub

尽管有条目可供使用,代码运行完美。 UI运行正常,用户可以导航。问题是当没有条目可用时,应用程序停留在LookForNewEntries循环并冻结整个UI。



不应该冻结UI,因为这个循环直接从一个线程而不是主线程运行?



无法找到任何解决方法来解决这个问题,任何人都可以给我一个提示?



谢谢!



祝福。

解决方案>

事实上,你实际上是从主线程运行它。



这个语句是你的问题:

 如果Me.InvokeRequired()然后
Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf IniciarDiscagem))
退出子
结束如果

您正在调用您的方法,使其在主线程上运行。你不应该使用调用,直到你要修改主线程上的东西。所以现在删除那段代码。



例如,如果要更改 TextBox 的文本'需要调用。



.NET 4.0及更高版本的示例:

  If Me.InvokeRequired Then 
Me.BeginInvoke(Sub()TextBox1.Text =Done)
如果

.NET 3.5及更低版本的示例:

 如果Me.InvokeRequired Then 
Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf< method here perform changes here>))
如果


I'm having trouble with mutli threading programming in my application. Actually, the code looks something like this:

Private Delegate Sub IniciarDiscagemDelegate()

Private Sub CallBack()
    'do some stuff....
    ThreadDiscagem = New Threading.Thread(AddressOf IniciarDiscagem)
    ThreadDiscagem.IsBackground = True
    ThreadDiscagem.Start()
End Sub

Private Sub IniciarDiscagem()
    If Me.InvokeRequired() Then
        Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf IniciarDiscagem))
        Exit Sub
    End If
    Do While True
       'This boolean is used to control navigation. I set false to it whenever the current entry is closed by the user.
       If Not blnEntryAlreadyOpen Then LookForNewEntries()
    Loop
End Sub

Private Sub LookForNewEntries()
    Dim blnFoundIt As Boolean = False
    Do While Not blnFoundIt
        blnFoundIt = DatabaseMethod()
    Loop
    'Some code that would change UI behavior, show some controls and hide others.
End Sub

Private Sub DataBaseMethod()
    'Code that looks for new entries at the DB
    return true
End Sub

While there are entries available to use, the code runs perfectly. The UI runs fine and the user can navigate. The problem is when there are no entries available, the application stucks at the "LookForNewEntries" loop and freezes the whole UI.

Isn't supposed to not freeze the UI since this loops run directly from a thread not than the main thread?

Could not find any workaround to this problem, can anyone give me a hint?

Thanks!

Best regards.

解决方案

In a matter of fact you are actually running it from the main thread.

This statement is your problem:

If Me.InvokeRequired() Then
    Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf IniciarDiscagem))
    Exit Sub
End If

You are invoking your method which will make it run on the main thread instead. You are not supposed to use invocation until you are going to modify something on the main thread. So remove that piece of code for now.

For example if you are to change the text of a TextBox you'd need to Invoke.

Example for .NET 4.0 and higher:

If Me.InvokeRequired Then
    Me.BeginInvoke(Sub() TextBox1.Text = "Done")
End If

Example for .NET 3.5 and lower:

If Me.InvokeRequired Then
    Me.BeginInvoke(New IniciarDiscagemDelegate(AddressOf <method to perform changes here>))
End If

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

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