DoEvents仅影响当前线程吗? [英] Does DoEvents effect only the current thread?

查看:112
本文介绍了DoEvents仅影响当前线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的工作"表单,该表单在其自己的线程上运行,以使用户知道该应用程序在长时间运行的操作中并未死亡.为了更新工作表,我不得不插入一个DoEvents()调用.

I have a simple 'Working' form that runs on its own thread to keep the user informed that the application hasn't died during long running operations. In order to get the working form to update I had to insert a DoEvents() call.

我很好奇,这只会为我所在的当前线程泵送消息,还是为整个应用程序执行?我希望主窗口在操作完成之前保持无响应,因此我对行为感到好奇.以下是工作表格的代码.

I'm curious, will this only pump messages for the current thread I'm in, or will it do it for the whole application? I would prefer that the main window stay unresponsive till the operation finishes, so I'm curious as to the behavior. Below is the code for the working form.

请清楚一点,我对我拥有的代码很好,但是我想知道DoEvents()在线程中的行为.

Just to be clear, I'm fine with the code I have, but I would like to know how DoEvents() behaves with threads.

    Public Class frmWorking

    ''' <summary>
    ''' Creates and starts a new thread to handle the Working Dialog
    ''' </summary>
    ''' <returns>The thread of the Working dialog.</returns>
    ''' <remarks></remarks>
    Public Shared Function StartWait() As WorkingFromToken
        Dim th As New Threading.Thread(AddressOf ShowWait)
        Dim token As New WorkingFromToken
        th.Start(token)
        Return token
    End Function

    Private Shared Sub ShowWait(token As WorkingFromToken)
        Dim frm As New frmWorking
        Try
            frm.Show()
            Do
                If frm.txtWait.Text.Length > 45 Then
                    frm.txtWait.Text = "Working"
                Else
                    frm.txtWait.Text &= "."
                End If
                Windows.Forms.Application.DoEvents()
                Threading.Thread.Sleep(250)
            Loop While token.Running
            frm.Hide()

        Catch ex As Threading.ThreadAbortException
            Threading.Thread.ResetAbort()
            frm.Hide()
            Return
        End Try

    End Sub

End Class

推荐答案

DoEvents将仅抽取当前UI线程.

DoEvents will only pump the current UI thread.

但是,我不建议您采用这种方法.

However, I do not recommend your approach.

相反,您应该在后台线程上进行工作,并在UI线程上显示模式进度表单,然后使用BeginInvokeBackgroundWorker对其进行更新.

Instead, you should do your work on a background thread, and show a modal progress form on the UI thread and update it using BeginInvoke or a BackgroundWorker.

这篇关于DoEvents仅影响当前线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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