如何在后台线程中运行代码并仍然访问 UI? [英] How can I run code in a background thread and still access the UI?

查看:33
本文介绍了如何在后台线程中运行代码并仍然访问 UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 .net lang 在 windows 10 上的 Visual Studio 中制作了一个文件搜索程序,我的问题从 form1 开始,带有dim frm2 as form2 = new form2"调用,在显示新表单后,我在 form1 上启动一个 while 循环,将数据输入表单 2 中的列表框:

I made a file search program in visual studio on windows 10 using .net lang, My problem starts from form1 with a "dim frm2 as form2 = new form2" call, after the new form being shown i start a while loop on form1 that feeds data into a listbox in form 2:

1)form1 调用 form2 并显示它.

1)form1 call form2 and show it.

2)form1 开始一个 while 循环.

2)form1 start a while loop.

3) 在 while 循环中,数据被馈送到 frm2 中的 listbox1

3)inside the while loop data being fed to listbox1 in frm2

现在在 windows 10 上一切正常,while 循环可以根据需要运行,没有任何问题,窗口可以失去焦点并重新获得焦点显示任何<代码>无响应.."消息或白黑屏幕..

Now everything works on windows 10, the while loop can run as much as it needs without any trouble, the window can loose focus and regain focus without showing any "Not Responding.." msgs or whitelack screens..

但是,当我将软件带到运行 windows 7 的朋友计算机时,安装所有必需的框架和 Visual Studio 本身,从 .sln 中运行它调试模式,在同一个文件夹上做同样的搜索,结果是:

But, when i take the software to my friend computer which is running windows 7, install all required frameworks and visual studio itself, run it from the .sln in debug mode, and do the same search on the same folder the results are:

1) 只要表单 2 没有失去焦点,while 循环就会顺利运行(Windows 10 上不会发生的事情)

1) the while loop runs smoothly as long as form 2 dont loose focus (something that doesnt happen on windows 10)

2) 当我点击屏幕上的任何地方时,软件失去焦点是什么导致1)发生(黑屏白屏无响应等..)

2) when i click anywhere on the screen the software loose focus what causes 1) to happen (black screenwhite screen ot responding etc..)

3) 如果我等待循环所需的时间并且不要点击其他任何地方它保持平稳运行,更新标签应该找到的文件数量......甚至以100% 成功完成循环(再次,除非我点击某处)

3) if i wait the time needed for the loop and dont click anywhere else it keeps running smoohtly, updating a label like it should with the amount of files found.. and even finish the loop with 100% success (again unless i click somewhere)

代码示例:

Sub ScanButtonInForm1()
    Dim frm2 As Form2 = New Form2
    frm2.Show()
    Dim AlreadyScanned As HashSet(Of String) = New HashSet(Of String)
    Dim stack As New Stack(Of String)
    stack.Push("...Directoy To Start The Search From...")
    Do While (stack.Count > 0)
        frm2.Label4.Text = "-- Mapping Files... -- Folders Left:" + stack.Count.ToString + " -- Files Found:" + frm2.ListBox1.Items.Count.ToString + " --"
        frm2.Label4.Refresh()
        Dim ScanDir As String = stack.Pop
        If AlreadyScanned.Add(ScanDir) Then
            Try
                Try
                    Try
                        Dim directoryName As String
                        For Each directoryName In System.IO.Directory.GetDirectories(ScanDir)
                            stack.Push(directoryName)
                            frm2.Label4.Text = "-- Mapping Files... -- Folders Left:" + stack.Count.ToString + " -- Files Found:" + frm2.ListBox1.Items.Count.ToString + " --"
                            frm2.Label4.Refresh()
                        Next
                        frm2.ListBox1.Items.AddRange(System.IO.Directory.GetFiles(ScanDir, "*.*", System.IO.SearchOption.AllDirectories))
                    Catch ex5 As UnauthorizedAccessException
                    End Try
                Catch ex2 As System.IO.PathTooLongException
                End Try
            Catch ex4 As System.IO.DirectoryNotFoundException
            End Try
        End If
    Loop
End Sub

我的结论很简单!

1) windows 7 不支持来自 while 循环的实时 ui(标签)更新从按钮调用...

1) windows 7 dont support live ui (label) update from a while loop called from a button...

2) Windows 7 可能支持新的线程运行相同的循环

2) windows 7 could possibly support a new thread running the same loop

我认为如果我在一个线程中运行所有代码,那么用户界面将保持响应

i think mabye if i run all the code in a thread mabye the ui will remain responsive

(顺便说一下,windows 10 中的 UI 没有响应,但我仍然看到标签刷新并且在形成松散焦点时没有任何崩溃...)

(by the way the UI is not responsive in windows 10 but i still see the label refresh and nothing crashes when form loose focus..)

所以我知道该怎么做,但我也知道如果我这样做,线程将无法更新表单中的列表框或标签并刷新它..

so i know how to do that but i also know that if i do that a thread will not be able to update a listbox or a label in a form and refresh it..

所以线程需要用数据更新一个外部文件,form2 需要从文件中实时读取该数据,但它会产生同样的问题吗?我不知道该怎么做..可以使用一些帮助和提示.谢谢!

so the thread will need to update an external file with the data and the form2 will need to read that data live from the file but will it make the same problems? i have no idea what to do.. can use some help and tips. THANK YOU!

我必须提到一个事实,即循环在 Windows 10 上运行而没有响应式 UI 意味着我无法点击任何按钮,但我可以仍然看到标签刷新但在 Windows 7 上一切都一样除非我点击某个地方,无论我在哪里点击 windows 循环崩溃

I must menttion the fact that the loop is working on windows 10 without a responsive UI means i cant click on any button but i can still see the label refresh BUT on windows 7 everything works the same UNLESS i click somewhere, no matter where i click on windows the loop crashes

我正在使用框架 4.6.2 开发人员

im using framework 4.6.2 developer

推荐答案

虽然我很高兴你找到了解决方案,但我建议不要使用 Application.DoEvents() 因为 这是不好的做法.

While I'm glad you found a solution, I advise against using Application.DoEvents() because it is bad practice.

请参阅此博文:保持 UI 响应和 Application.DoEvents 的危险.

Please see this blog post: Keeping your UI Responsive and the Dangers of Application.DoEvents.

简单地说,Application.DoEvents() 是一种肮脏的解决方法,它使您的 UI 看起来响应迅速,因为它强制 UI 线程处理所有当前可用的窗口消息.WM_PAINT 是这些消息之一,这就是您的窗口重绘的原因.

Simply put, Application.DoEvents() is a dirty workaround that makes your UI seem responsive because it forces the UI thread to handle all currently available window messages. WM_PAINT is one of those messages which is why your window redraws.

然而,这有一些背后......例如:

However this has some backsides to it... For instance:

  • 如果你在这个后台"过程中关闭表单,它很可能会抛出一个错误.

  • If you were to close the form during this "background" process it would most likely throw an error.

另一个背后是,如果 ScanButtonInForm1() 方法是通过单击按钮调用的,您将能够再次单击该按钮(除非您设置 Enabled =False) 并再次开始这个过程,这将我们带到另一个背面:

Another backside is that if the ScanButtonInForm1() method is called by the click of a button you'd be able to click that button again (unless you set Enabled = False) and starting the process once more, which brings us to yet another backside:

您启动的 Application.DoEvents() 循环越多,您占用的 UI 线程就越多,这将导致您的 CPU 使用率上升得相当快.由于每个循环都在同一个线程中运行,您的处理器无法在不同的内核或线程上安排工作,因此您的代码将总是在一个内核上运行,消耗的 CPU 与可能.

The more Application.DoEvents()-loops you start the more you occupy the UI thread, which will cause your CPU usage to rise rather quickly. Since every loop is run in the same thread your processor cannot schedule the work over different cores nor threads, so your code will always run on one core, eating as much CPU as possible.

当然,替代的是适当的多线程(或任务并行库,无论您喜欢哪个).常规的多线程实际上并不难实现.

The replacement is, of course, proper multithreading (or the Task Parallel Library, whichever you prefer). Regular multithreading actually isn't that hard to implement.


为了创建一个新线程,您只需要声明一个 Thread 并将委托传递给您希望线程运行的方法:

In order to create a new thread you only need to declare an instance of the Thread class and pass a delegate to the method you want the thread to run:

Dim myThread As New Thread(AddressOf <your method here>)

...那么你应该设置它的 IsBackground 属性True 如果您希望它在程序关闭时自动关闭(否则它会保留程序打开直到线程结束).

...then you should set its IsBackground property to True if you want it to close automatically when the program closes (otherwise it keeps the program open until the thread finishes).

然后你只需调用 Start() 并且您有一个正在运行的后台线程!

Then you just call Start() and you have a running background thread!

Dim myThread As New Thread(AddressOf myThreadMethod)
myThread.IsBackground = True
myThread.Start()


多线程的棘手部分是将调用编组到 UI 线程.后台线程通常无法访问 UI 线程上的元素(控件),因为这可能会导致并发问题(两个线程同时访问同一控件).因此,您必须通过安排在 UI 线程本身上执行来编组对 UI 的调用.这样您就不会再有并发风险,因为所有 UI 相关代码都在 UI 线程上运行.

The tricky part about multithreading is to marshal calls to the UI thread. A background thread generally cannot access elements (controls) on the UI thread because that might cause concurrency issues (two threads accessing the same control at the same time). Therefore you must marshal your calls to the UI by scheduling them for execution on the UI thread itself. That way you will no longer have the risk of concurrency because all UI related code is run on the UI thread.

要对 UI 线程进行编组调用,请使用 Control.Invoke()Control.BeginInvoke() 方法.BeginInvoke()异步版本,这意味着它不会等待 UI 调用完成才让后台线程继续其工作.

To marhsal calls to the UI thread you use either of the Control.Invoke() or Control.BeginInvoke() methods. BeginInvoke() is the asynchronous version, which means it doesn't wait for the UI call to complete before it lets the background thread continue with its work.

还应确保检查 Control.InvokeRequired 属性,它告诉您是否已经在 UI 线程上(在这种情况下,调用是非常不必要)或不.

One should also make sure to check the Control.InvokeRequired property, which tells you if you already are on the UI thread (in which case invoking is extremely unnecessary) or not.

基本的 InvokeRequired/Invoke 模式看起来像这样(主要是为了参考,请继续阅读下面的简短方法):

The basic InvokeRequired/Invoke pattern looks like this (mostly for reference, keep reading below for shorter ways):

'This delegate will be used to tell Control.Invoke() which method we want to invoke on the UI thread.
Private Delegate Sub UpdateTextBoxDelegate(ByVal TargetTextBox As TextBox, ByVal Text As String)

Private Sub myThreadMethod() 'The method that our thread runs.
    'Do some background stuff...

    If Me.InvokeRequired = True Then '"Me" being the current form.
        Me.Invoke(New UpdateTextBoxDelegate(AddressOf UpdateTextBox), TextBox1, "Status update!") 'We are in a background thread, therefore we must invoke.
    Else
        UpdateTextBox(TextBox1, "Status update!") 'We are on the UI thread, no invoking required.
    End If

    'Do some more background stuff...
End Sub

'This is the method that Control.Invoke() will execute.
Private Sub UpdateTextBox(ByVal TargetTextBox As TextBox, ByVal Text As String)
    TargetTextBox.Text = Text
End Sub

New UpdateTextBoxDelegate(AddressOf UpdateTextBox) 创建 UpdateTextBoxDelegate 的新实例,该实例指向我们的 UpdateTextBox 方法(在用户界面).

New UpdateTextBoxDelegate(AddressOf UpdateTextBox) creates a new instance of the UpdateTextBoxDelegate that points to our UpdateTextBox method (the method to invoke on the UI).

但是,从 Visual Basic 2010 (10.0) 及更高版本开始,您可以使用 Lambda 表达式 这使得调用很多更容易:

Private Sub myThreadMethod()
    'Do some background stuff...

    If Me.InvokeRequired = True Then '"Me" being the current form.
        Me.Invoke(Sub() TextBox1.Text = "Status update!") 'We are in a background thread, therefore we must invoke.
    Else
        TextBox1.Text = "Status update!" 'We are on the UI thread, no invoking required.
    End If

    'Do some more background stuff...
End Sub

现在你要做的就是输入 Sub() 然后继续输入代码,就像你在常规方法中一样:

Now all you have to do is type Sub() and then continue typing code like if you were in a regular method:

If Me.InvokeRequired = True Then
    Me.Invoke(Sub()
                  TextBox1.Text = "Status update!"
                  Me.Text = "Hello world!"
                  Label1.Location = New Point(128, 32)
                  ProgressBar1.Value += 1
              End Sub)
Else
    TextBox1.Text = "Status update!"
    Me.Text = "Hello world!"
    Label1.Location = New Point(128, 32)
    ProgressBar1.Value += 1
End If

这就是您对 UI 线程进行编组调用的方式!

And that's how you marshal calls to the UI thread!


为了甚至更简单地调用 UI,您可以创建一个 扩展方法 为您执行调用和 InvokeRequired 检查.

To make it even more simple to invoke to the UI you can create an Extension method that does the invoking and InvokeRequired check for you.

将其放在单独的代码文件中:

Place this in a separate code file:

Imports System.Runtime.CompilerServices

Public Module Extensions
    ''' <summary>
    ''' Invokes the specified method on the calling control's thread (if necessary, otherwise on the current thread).
    ''' </summary>
    ''' <param name="Control">The control which's thread to invoke the method at.</param>
    ''' <param name="Method">The method to invoke.</param>
    ''' <param name="Parameters">The parameters to pass to the method (optional).</param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function InvokeIfRequired(ByVal Control As Control, ByVal Method As [Delegate], ByVal ParamArray Parameters As Object()) As Object
        If Parameters IsNot Nothing AndAlso _
            Parameters.Length = 0 Then Parameters = Nothing

        If Control.InvokeRequired = True Then
            Return Control.Invoke(Method, Parameters)
        Else
            Return Method.DynamicInvoke(Parameters)
        End If
    End Function
End Module

现在你只需要在你想要访问 UI 时调用这个方法,不需要额外的 If-Then-Else:

Now you only need to call this single method when you want to access the UI, no additional If-Then-Else required:

Private Sub myThreadMethod()
    'Do some background stuff...

    Me.InvokeIfRequired(Sub()
                            TextBox1.Text = "Status update!"
                            Me.Text = "Hello world!"
                            Label1.Location = New Point(128, 32)
                        End Sub)

    'Do some more background stuff...
End Sub


使用我的 InvokeIfRequired() 扩展方法,您还可以以简单的方式从 UI 线程返回对象或数据.例如,如果您想要标签的宽度:

With my InvokeIfRequired() extension method you can also return objects or data from the UI thread in a simple manner. For instance if you want the width of a label:

Dim LabelWidth As Integer = Me.InvokeIfRequired(Function() Label1.Width)


以下代码将增加一个计数器,告诉您线程运行了多长时间:

The following code will increment a counter that tells you for how long the thread has run:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim CounterThread As New Thread(AddressOf CounterThreadMethod)
    CounterThread.IsBackground = True
    CounterThread.Start()

    Button1.Enabled = False 'Make the button unclickable (so that we cannot start yet another thread).
End Sub

Private Sub CounterThreadMethod()
    Dim Time As Integer = 0

    While True
        Thread.Sleep(1000) 'Wait for approximately 1000 ms (1 second).
        Time += 1

        Me.InvokeIfRequired(Sub() Label1.Text = "Thread has been running for: " & Time & " seconds.")
    End While
End Sub


希望这有帮助!

这篇关于如何在后台线程中运行代码并仍然访问 UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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