[英]how to run two process at a time in vb.net?
Imports System.Threading.Thread
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
While i <= 10
TextBox1.Text = i.ToString()
TextBox1.Refresh()
Sleep(1000)
i = i + 1
End While
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim lbl As Label
Dim matches() As Control
matches = Me.Controls.Find("Label" & TextBox1.Text, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
lbl = DirectCast(matches(0), Label)
lbl.BackColor = Color.Yellow
End If
End Sub
End Class
If i click button1 then it is showing 1 to 10 in textbox each after one second. 如果我单击button1,则一秒钟后它在文本框中显示1到10。 It is working fine. 一切正常。 after clicked button1 when textbox.text=2 that time i want to press button2. 在单击button1后,当textbox.text = 2时,我想按一下button2。 if i press button2 then it should show the label2.backcolor=yellow. 如果我按下button2,那么它应该显示label2.backcolor = yellow。 label no is textbox.text. 标签编号为textbox.text。 Problem is : After finished while loop in button1 then only i can press button2. 问题是:在button1中循环完成后,只有我可以按下button2。 we are getting only label10 as yellow color after finished while loop. 在while循环结束后,我们只会得到黄色的label10。 Solution : I want to click that button2 before ending while loop. 解决方案:我想在while循环结束之前单击该button2。 Please give solution. 请解决。 Actually i want which number is showing in textbox1.text that label should be yellow color. 实际上,我想在textbox1.text中显示哪个数字,标签应该是黄色。
Your While
loop in Button1.Click
is blocking the UI thread, and therefore blocking the Button2.Click
handler until Button1.Click
finishes. Button1.Click
While
循环正在阻止UI线程,因此将阻止Button2.Click
处理程序,直到Button1.Click
完成。 One simple solution would be to make Button1.Click
Async
(assuming .NET 4.5+) and use Task.Delay(1000)
instead: 一个简单的解决方案是制作Button1.Click
Async
(假定.NET 4.5+),然后使用Task.Delay(1000)
代替:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
While i <= 10
TextBox1.Text = i.ToString()
TextBox1.Refresh()
'Sleep(1000)
Await Task.Delay(1000)
i = i + 1
End While
End Sub
This will free up the UI thread when the Await
is hit. 按下“ Await
后,这将释放UI线程。
You could also use a background thread, but then you would have to ensure that your UI updates happen on the UI thread. 您也可以使用后台线程,但随后必须确保UI更新在UI线程上进行。
Warning: terrible code follows (but it works!) 警告:出现可怕的代码(但它可以工作!)
Private Sub Wait(ByVal time As Integer) 'in milliseconds
Dim secs As Integer = Environment.TickCount
While Environment.TickCount < secs + time
Application.DoEvents()
End While
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim i As Integer
While i <= 10
Label1.Text = i.ToString()
Label1.Refresh()
Wait(1000)
i = i + 1
End While
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Static toggle As Boolean
If toggle Then
Label1.BackColor = Color.Green
Else
Label1.BackColor = Color.Blue
End If
toggle = Not toggle
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.