假设一个循环在自己的线程上运行。 在循环语句中的某处我有条件要检查。如果条件返回True,那么我需要打开一个新的对话框窗口,其中有2个选项供用户选择。新对话框窗口必须阻止新主线程和主线程,以防止用户导航到系统,直到用户选择其选项。 当我使用带有以下代码的Windows窗体
时,我能够完成此操作 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim thr As New System.Threading.Thread(AddressOf start)
thr.Start()
End Sub
Private Sub start()
For i As Integer = 0 To 10000
'Do some tasks here
'
'
'
'
'
If TobeAskUser = True Then
Dim win As New Form
Me.Invoke(New MethodInvoker(AddressOf win.ShowDialog))
If win.DialogResult = Windows.Forms.DialogResult.OK Then
Continue For
Else
Exit Sub
End If
End If
Next
End Sub
**现在我和WPF合作,经过几种方式我无法实现**。
EDIT 以下代码适用于wpf。但不确定我是否做得正确
Dim win As New ReplaceFileAttachment ' <- this is a window
Private Sub Start()
For i As Integer = 0 To 1000
'do some task here
'
'
'
'
If IsSelected = True Then
Me.Dispatcher.Invoke(DirectCast(Sub() win = New Window, Action))
Me.Dispatcher.Invoke(DirectCast(Sub() win.ShowDialog(), Action))
'the dialog above will be open and block and the new thread and the main thread. so the user canot navigate around the system until it close it
Select Case win.CustomDialog '<- custom dialog it's a custom enum property.
Case ReplaceFileAttachment.customDialog_.AddAsNew
Case ReplaceFileAttachment.customDialog_.Cancel
Case ReplaceFileAttachment.customDialog_.Replace
End Select
End If
Next
End Sub