我为一些长期运行的操作创建了一个奇特的进度对话框.我需要在新线程上显示此对话框,同时在调用(大多数情况下为UI)线程上继续处理.
这有3个真正的要求:
>阻止用户与调用表单交互(类似于ShowDialog(this))
>将进度对话框保持在主窗口上方(现在可能落后)
>允许主线程继续处理
我看起来像这样(到目前为止工作得很好,就运行而言,除了上面的那些问题):
Using ... ShowNewProgressDialogOnNewThread() ...
Logic
UpdateProgress() //static
Logic
UpdateProgress() //static, uses Invoke() to call dialog
...
End Using // destroys the form, etc
我尝试了几种方法来做到这一点:
> BackgroundWorker / Thread上的ShowDialog()
> Action.BeginInvoke()调用一个函数
> ProgressForm.BeginInvoke(…调用ShowDialog的方法……)
>在实现IWin32Window的类中包装main表单,因此它可以被称为交叉线程并传递给ShowDialog() – 这个稍后在某个地方失败了,但至少会导致ShowDialog()不立即barf.
关于如何使这项工作的任何线索或智慧?
解决方案(现在)
>对EnableWindow的调用正是我所寻求的.
>我根本没有遇到任何崩溃
>更改为使用ManualResetEvent
>我设置了TopMost,因为我不能总是保证表格会最终排在最前面.也许有更好的方法.
>我的进度表格就像一个闪屏(没有大小,没有工具栏等),也许是因为缺少崩溃(在回答中提到)
>这里是another thread on the EnableWindow topic(没有参考此修复,tho)
但是现在出现了一个新问题,一旦尝试向其所有者发送消息,进度窗口就会逐渐消失.有点令人惊讶的是,当您使用Invoke()而不是BeginInvoke()来更新进度时,这个问题有点消失.有点,您仍然可以通过将鼠标移动到禁用所有者的边框上来解决问题.实际上,你必须使用TopMost来确定Z顺序.更现实的是,Windows不支持您尝试做的事情.你知道真正的解决方案,它是你问题的首要问题.
这是一些要试验的代码.假设你的进度形式叫做dlgProgress:
Imports System.Threading
Public Class ShowProgress
Implements IDisposable
Private Delegate Sub UpdateProgressDelegate(ByVal pct As Integer)
Private mOwnerHandle As IntPtr
Private mOwnerRect As Rectangle
Private mProgress As dlgProgress
Private mInterlock As ManualResetEvent
Public Sub New(ByVal owner As Form)
Debug.Assert(owner.Created)
mOwnerHandle = owner.Handle
mOwnerRect = owner.Bounds
mInterlock = New ManualResetEvent(False)
Dim t As Thread = New Thread(AddressOf dlgStart)
t.SetApartmentState(ApartmentState.STA)
t.Start()
mInterlock.WaitOne()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
mProgress.BeginInvoke(New MethodInvoker(AddressOf dlgClose))
End Sub
Public Sub UpdateProgress(ByVal pct As Integer)
mProgress.Invoke(New UpdateProgressDelegate(AddressOf dlgUpdate), pct)
End Sub
Private Sub dlgStart()
mProgress = New dlgProgress
mProgress.StartPosition = FormStartPosition.Manual
mProgress.ShowInTaskbar = False
AddHandler mProgress.Load, AddressOf dlgLoad
AddHandler mProgress.FormClosing, AddressOf dlgClosing
EnableWindow(mOwnerHandle, False)
SetWindowLong(mProgress.Handle, -8, mOwnerHandle)
Application.Run(mProgress)
End Sub
Private Sub dlgLoad(ByVal sender As Object, ByVal e As EventArgs)
mProgress.Location = New Point( _
mOwnerRect.Left + (mOwnerRect.Width - mProgress.Width) \ 2, _
mOwnerRect.Top + (mOwnerRect.Height - mProgress.Height) \ 2)
mInterlock.Set()
End Sub
Private Sub dlgUpdate(ByVal pct As Integer)
mProgress.ProgressBar1.Value = pct
End Sub
Private Sub dlgClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
EnableWindow(mOwnerHandle, True)
End Sub
Private Sub dlgClose()
mProgress.Close()
mProgress = Nothing
End Sub
'--- P/Invoke
Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As IntPtr) As IntPtr
If IntPtr.Size = 4 Then
Return SetWindowLongPtr32(hWnd, nIndex, dwNewLong)
Else
Return SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
End If
End Function
Private Declare Function EnableWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal enabled As Boolean) As Boolean
Private Declare Function SetWindowLongPtr32 Lib "user32.dll" Alias "SetWindowLongW" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As IntPtr) As IntPtr
Private Declare Function SetWindowLongPtr64 Lib "user32.dll" Alias "SetWindowLongW" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As IntPtr) As IntPtr
End Class
样品用法:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using dlg As New ShowProgress(Me)
For ix As Integer = 1 To 100
dlg.UpdateProgress(ix)
System.Threading.Thread.Sleep(50)
Next
End Using
End Sub
