我有两种形式
1- 按Form1中的Button1
2- 打开Form2(等待表格)
3- 如果保存(Form1中的Button1)已完成,则关闭Form2(表单等待)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
For aloop As Integer = 0 To 1000000
Application.DoEvents()
Label1.Text = Now
Next
Form2.Close()
End Sub
答案 0 :(得分:2)
你 不应该 使用Application.DoEvents()
来保持用户界面的响应能力! EVER! 这样做 非常糟糕的做法!
请阅读:Keeping your UI responsive and the Dangers of Application.DoEvents
相反,你应该使用多线程(已经建议,我知道 - 但是没有使用Application.DoEvents()
)。该主题需要做两件事:运行保存代码并在完成后关闭Form2
。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Enabled = False
Dim t As New Thread(AddressOf SaveThread)
t.IsBackground = True
t.Start()
End Sub
Private Sub SaveThread()
'Put your save code here and remove Thread.Sleep() below.
Thread.Sleep(6000) 'Simulating delay, 6000 ms = 6 seconds.
If Me.InvokeRequired = True Then
Me.Invoke(New MethodInvoker(AddressOf CloseWaitingForm))
Else
CloseWaitingForm()
End If
End Sub
Private Sub CloseWaitingForm()
Form2.Close()
Me.Enabled = True
End Sub
就这么简单。
答案 1 :(得分:0)
不是使用key:Content-Type, value:application/json
key:Authorization:key=<Server key>
,而是使用此方法,它们具有相同的效果:
Form2.ShowDialog()
就像这样,Form1将被禁用,直到保存完成并且Form2关闭,就像Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Enable = False
For aloop As Integer = 0 To 1000000
Application.DoEvents()
Label1.Text = Now
Next
Form2.Close()
Me.Enable = True
End Sub
工作时会发生的那样。
希望对你有所帮助:)
答案 2 :(得分:0)
您不希望用户在保存时与Form1
进行互动,对吧?
好吧,您可以暂时禁用该表单,而不是使用.ShowDialog()
。
看看这个:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'show your "Wait" form
Form3.Show()
'disable the current form
Me.Enabled = False
'do your work
For aloop As Integer = 0 To 100000
Application.DoEvents()
TextBox1.Text = Now
Next
'close the "wait" form once it's saved
Form3.Close()
'enable the current form
Me.Enabled = True
'set focus to the current form
Me.Focus()
End Sub
在发布之前我亲自尝试过。表格最小化,没有.Focus()
,所以我把它放在那里。
希望这能解决你的问题。
答案 3 :(得分:0)
代码的问题是.ShowDialog()方法,因为它会在关闭之前停止执行以下命令。 尝试使用.Show()而不是.ShowDialog()并禁用Form1作为其他建议的答案。
如果您仍想使用.ShowDialog(),我建议您使用 Threading.Thread
以下是Form1中可以做的事情
Public Class Form1
Public tr As Threading.Thread
Private Sub yourMethod()
For aloop As Integer = 0 To 1000000
If Label1.InvokeRequired Then
Label1.Invoke(Sub() Label1.Text = Now)
Else
Label1.Text = Now
End If
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
tr = New Threading.Thread(Sub() yourMethod())
tr.Start()
Form2.ShowDialog()
End Sub
End Class
在Form2中:
Public Class Form2
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Do While Form1.tr.IsAlive
Application.DoEvents()
Loop
Me.Close()
End Sub
End Class
以上代码执行以下操作: