Response.Flush不工作

时间:2014-05-15 20:23:02

标签: .net vb.net

请参阅以下代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Response.Buffer = False
        For i As Integer = 0 To 5
            Label1.Text = Label1.Text & " " & i
            Response.Flush()
            Threading.Thread.Sleep(2000)
        Next
    End Sub

我希望这打印0,然后等待两秒,然后打印0 1然后等待两秒等。但是,它只是等待12秒并打印所有内容。我做错了什么?

1 个答案:

答案 0 :(得分:0)

在上面的代码中,它将使主线程休眠2秒,因为在完成代码执行之前,您无法看到数据或表单。您可以使用计时器,并将间隔设置为2000。喜欢..

   Dim i As Integer = 0
   ' You have to Start Timer1 like, Timer1.Enabled = True
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If i >= 6 Then  
            Timer1.Enabled = False
            i = 0
        Else
            Label1.Text = Label1.Text & " " & i
            ' Code here to execute for 0 to 5
            i += 1
        End If

    End Sub