none
Progressbar運作問題 RRS feed

  • 問題

  • 使用了Button.click呼叫一個From,From的dowork有一SQL執行時間較長,所以要在結果出來前讓Progressbar跑動,讓使用者覺得是再等待結果出來
    ,並在SQL將結果傳回後關閉這個From,目前結果可正確傳回,但就是這個Progressbar並不會跑,煩請各位先進指點一下,謝謝

    Public Class Rcvr_Progressbar

        Dim BARCOUNT As Integer = 0
        Dim n As Integer
        Dim value As String = frmMain.TextBox5.Text

        Private Sub Rcvr_Progressbar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = 10
            ProgressBar1.Value = 0

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim i As Integer
            For i = 1 To 100
                ProgressBar1.Value += 1
                System.Threading.Thread.Sleep(70)
                If ProgressBar1.Value >= ProgressBar1.Maximum Then
                    i = 0
                    ProgressBar1.Value = 0
                End If
            Next
        End Sub

        Public Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

            '計算總數()
            '------------------------------------------------------
            selectQuery_Rcv_Count = "Select Count(PhoneNumber) COUNT from NP_CORE_MSISDN"
            selectQuery_Rcv_Count = selectQuery_Rcv_Count & " where (((STATE = 1) AND (24 * (RIUpdateDate - CreateDate) > 1)) OR ((STATE = 2) OR (STATE = 3)))"
            selectQuery_Rcv_Count = selectQuery_Rcv_Count & " and phonenumber like '" & value & "%'"
            ors.open(selectQuery_Rcv_Count, conn)

            BARCOUNT = ors("COUNT").value()
            ors.close()
            MsgBox("共" & BARCOUNT & "筆")
            '------------------------------------------------------

        End Sub

     

        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

            Me.Dispose()
            Me.Close()

        End Sub
    End Class

    2009年4月29日 上午 09:46

解答

  • Orange:
       1.   你的程式會變成無窮迴圈
        Dim i As Integer
            For i = 1 To 100
                ProgressBar1.Value += 1
                System.Threading.Thread.Sleep(70)
                If ProgressBar1.Value >= ProgressBar1.Maximum Then
                    'i = 0  <===這一行要拿掉 ,不然 永遠跳不出迴圈
                    ProgressBar1.Value = 0
                End If
            Next
       
        2.我試著寫一個類似的程式, ProgressBar一樣會更新.是否正如Angusruby所說你沒有啟動Timer
           Public Class Form1
        Dim FormTimer As New Windows.Forms.Timer
        Private Sub BTN_FrmT_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_FrmT_start.Click
            FormTimer.Interval = 2000
            AddHandler FormTimer.Tick, AddressOf myTimerTick02
            FormTimer.Start()
            BackgroundWorker1.RunWorkerAsync()
        End Sub
        Private Sub myTimerTick02(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim i As Integer
            For i = 1 To 100
                ProgressBar1.Value += 1
                System.Threading.Thread.Sleep(10)
                If ProgressBar1.Value >= ProgressBar1.Maximum Then
                    ' i = 0
                    ProgressBar1.Value = 0
                End If
                Application.DoEvents()
            Next
        End Sub

        Private Sub BTN_FrmT_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_FrmT_stop.Click
            FormTimer.Stop()
        End Sub

        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim i As Integer
            Dim j As Integer
            For i = 0 To 1000
                For j = 0 To 10000000

                Next
            Next
        End Sub

        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            FormTimer.Stop()
            MessageBox.Show("Done!!")
        End Sub
    End Class
        

    • 已提議為解答 Lolota Lee 2009年4月30日 上午 10:24
    • 已標示為解答 Lolota Lee 2009年5月5日 上午 01:39
    2009年4月29日 下午 02:46
    版主

所有回覆

  • 您好 ,
    您好像沒有啟動Time1哦

    在Load的事件中加入Time1.Enable試試吧

    2009年4月29日 上午 09:51
  • 去微軟下載中心抓 VB2005 的 101 sample ,裡面的 MultiThread 有 3 種多緒處理進度列的方式。
    論壇是網友平等互助 保證解答請至 微軟技術支援服務
    2009年4月29日 下午 01:36
  • Orange:
       1.   你的程式會變成無窮迴圈
        Dim i As Integer
            For i = 1 To 100
                ProgressBar1.Value += 1
                System.Threading.Thread.Sleep(70)
                If ProgressBar1.Value >= ProgressBar1.Maximum Then
                    'i = 0  <===這一行要拿掉 ,不然 永遠跳不出迴圈
                    ProgressBar1.Value = 0
                End If
            Next
       
        2.我試著寫一個類似的程式, ProgressBar一樣會更新.是否正如Angusruby所說你沒有啟動Timer
           Public Class Form1
        Dim FormTimer As New Windows.Forms.Timer
        Private Sub BTN_FrmT_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_FrmT_start.Click
            FormTimer.Interval = 2000
            AddHandler FormTimer.Tick, AddressOf myTimerTick02
            FormTimer.Start()
            BackgroundWorker1.RunWorkerAsync()
        End Sub
        Private Sub myTimerTick02(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim i As Integer
            For i = 1 To 100
                ProgressBar1.Value += 1
                System.Threading.Thread.Sleep(10)
                If ProgressBar1.Value >= ProgressBar1.Maximum Then
                    ' i = 0
                    ProgressBar1.Value = 0
                End If
                Application.DoEvents()
            Next
        End Sub

        Private Sub BTN_FrmT_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_FrmT_stop.Click
            FormTimer.Stop()
        End Sub

        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim i As Integer
            Dim j As Integer
            For i = 0 To 1000
                For j = 0 To 10000000

                Next
            Next
        End Sub

        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            FormTimer.Stop()
            MessageBox.Show("Done!!")
        End Sub
    End Class
        

    • 已提議為解答 Lolota Lee 2009年4月30日 上午 10:24
    • 已標示為解答 Lolota Lee 2009年5月5日 上午 01:39
    2009年4月29日 下午 02:46
    版主
  • 已測試成功,感謝各位前輩的協助

    2009年4月30日 上午 08:54