DoEvents仅影响当前线程吗?

编程入门 行业动态 更新时间:2024-10-19 19:23:48
本文介绍了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线程上显示模式进度表单,然后使用BeginInvoke或BackgroundWorker对其进行更新.

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仅影响当前线程吗?

本文发布于:2023-11-09 20:47:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   DoEvents

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!