Why doesn't my flow start right away?

see below program. I start a new thread x with the abc function, then do a longer task. Why does x only start after it finishes? Shouldn't you start right away, before going to bed?

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New Threading.Thread(AddressOf abc)
        x.SetApartmentState(Threading.ApartmentState.MTA)
        x.Start()

        System.Threading.Thread.Sleep(5000)
    End Sub





Sub abc()
    For i As Integer = 0 To 10 Step 1
        Me.lblStatus.Text = "Testing DB connection ( timeout in: " + i.ToString() + "s )"
        'Me.StatusStrip1.Invoke(
        MsgBox(i.ToString)
        System.Threading.Thread.Sleep(1000)
    Next
End Sub



Edit:
The solution is this:

(A) Put both connection attempts and timeout countdown in separate streams.
(B) Update the user interface as follows:

    If Me.InvokeRequired Then
        Me.Invoke(pUpdateStatusMessage, "Successfully connected.")
    Else
        UpdateStatusMessage("Successfully connected.")
    End If

If this is declared globally, so passing arguments is not required:

Delegate Sub t_pUpdateStatusText(ByVal strMessage As String)
Public pUpdateStatusMessage As t_pUpdateStatusText = New t_pUpdateStatusText(AddressOf UpdateStatusMessage)

Public Sub UpdateStatusMessage(ByVal strMessage As String)
    Me.lblStatus.Text = strMessage
    Me.StatusStrip1.Update()
End Sub
+5
source share
4 answers

abc Button1_Click. , .

-,

Me.lblStatus.Text = "Testing DB connection ( timeout in: " + i.ToString() + "s )" 

. Invoke . , .

Invoke . , , Windows . Thread.Sleep, , . , Sleep , , .

+10

, UI. . , , .

+2

, , ( , , ), , , .

- :

private delegate void ControlUpdateTextHandler(TextBox ctrl, string text);
public void UpdateControlText(TextBox ctrl, string text)
{
    if (ctrl.InvokeRequired)
    {
        ctrl.BeginInvoke((ControlUpdateTextHandler)UpdateControlText, ctrl, text);
    }
    else
        ctrl.Text = text;
}

, "" ( InvokeRequired , , ), "" (becusue InvokeRequired true, ), , .

0

, , BackgroundWorker. abc() DoWork ProgessChanged .

0

All Articles