visual basic中的线程计时

时间:2014-10-22 00:48:32

标签: vb.net winforms

我正在使用http请求从本地Web服务器检查帐户状态,我在PHP中添加了一个小延迟来模仿远程服务器的行为。

我正在尝试在WebRequest正在进行时启用微调器,我知道代码的所有功能都可以工作,但我的问题是spinner(spinthread)在WebRequest之前不会显示(reqthread) ) 完成了。

Dim spinthread As System.Threading.Thread
Dim reqthread As System.Threading.Thread
Private Delegate Sub spinDelegate()
Private Delegate Sub reqDelegate()

Private Sub spintog() 'spinnner toggle
    If Me.InvokeRequired Then
        Me.Invoke(New spinDelegate(AddressOf spintog))
    Else
        If MetroProgressSpinner1.Visible = True Then
            MetroProgressSpinner1.Visible = False

        Else

            MetroProgressSpinner1.Visible = True

        End If
    End If
End Sub


Private Sub reqsub()
    If Me.InvokeRequired Then
        Me.Invoke(New reqDelegate(AddressOf reqsub))
    Else

        ' Create a request using a URL that can receive a post. 
        Dim request As WebRequest = WebRequest.Create("http://localhost/json/login.php ")
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = "username=" & username.Text & "&password=" & password.Text
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        Dim loginresponse As String = responseFromServer

        debug.Text = loginresponse


        If loginresponse.Contains("fail") Then
            faillabel.Visible = True
        Else
            faillabel.Visible = False


        End If

        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()

    End If
End Sub

Private Sub login_Click(sender As Object, e As EventArgs) Handles login.Click

    spinthread = New System.Threading.Thread(AddressOf spintog)
    spinthread.Start()

    reqthread = New System.Threading.Thread(AddressOf reqsub)
    reqthread.Start()

End Sub

0 个答案:

没有答案