简体   繁体   中英

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. It is working fine. after clicked button1 when textbox.text=2 that time i want to press button2. if i press button2 then it should show the label2.backcolor=yellow. label no is textbox.text. Problem is : After finished while loop in button1 then only i can press button2. we are getting only label10 as yellow color after finished while loop. Solution : I want to click that button2 before ending while loop. Please give solution. Actually i want which number is showing in textbox1.text that label should be yellow color.

Your While loop in Button1.Click is blocking the UI thread, and therefore blocking the Button2.Click handler until Button1.Click finishes. One simple solution would be to make Button1.Click Async (assuming .NET 4.5+) and use Task.Delay(1000) instead:

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.

You could also use a background thread, but then you would have to ensure that your UI updates happen on the UI thread.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM