none
请问关于backgroundworker不能关掉的问题,急急急,救命啊 RRS feed

  • 問題

  • 我弄了个backgroundworker在后台运行,它的目的是让一个textbox不停地去显示不同的内容。但是我用cancel停止不了它,我的代码如下,请高人指教

    第一个是backgroundworker要做的那个work的类:
    Imports System.threading
    Public Class MyTimer
        Public Class workState
            Public total = 60
            Public alwaysChange = 60
            Public result As Single
            Public fraction As Integer
            Public tempResult As String
            Public intPart As String
        End Class

     

        Public Sub Work(ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
            Dim currentState As workState
            currentState = New workState
            currentState.total = 60
            currentState.alwaysChange = 60

            Do Until currentState.alwaysChange = 0
                currentState.alwaysChange = currentState.alwaysChange - 1
                currentState.result = currentState.alwaysChange / currentState.total
                currentState.fraction = currentState.alwaysChange Mod currentState.total
                currentState.tempResult = currentState.result.ToString()
                currentState.tempResult.Split(".")
                currentState.intPart = currentState.tempResult(0)
                display.Text = temp1 + ":" + d.ToString
                worker.ReportProgress(0, currentState)
                Thread.Sleep(1000)
            Loop
        End Sub
    End Class

    第二个是主form里的类:
    Imports System.ComponentModel
    Imports System.threading


    Public Class Main
         Dim gameTimer As New MyTimer
            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           

           Me.BackgroundWorker1.WorkerSupportsCancellation = True
            me.backgroundWorker1.workerreportprogress=true


         End Sub
        
        Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click

            BackgroundWorker1.RunWorkerAsync(gameTimer)
        End Sub
        
        Private Sub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click
                BackgroundWorker1.CancelAsync()
         End Sub
       
       Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork


            Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
            Dim workTimer As MyTimer = CType(e.Argument, MyTimer)
            workTimer.Work(worker, e)


        End Sub

     

        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

            Dim state As MyTimer.workState = CType(e.UserState, MyTimer.workState)
            timer.Text = state.intPart + ":" + state.fraction.ToString
            '  MsgBox("fasdf")
        End Sub

     

        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            If  e.Cancelled Then
                MsgBox("Cancel")
                   End If
        End Sub
    End Class

    还有一个问题是如何让这个线程暂停?暂停后如何使它resume?请高人指教,先谢谢了
    2008年10月17日 下午 02:15

解答

  • (1)
    將BackgroundWorker的WorkerSupportsCancellation屬性設為True,
    表示此BackgroundWorker物件可以支援取消作業的動作﹝也就是使它可以呼叫CancelAsync方法﹞。

    (2)
    在某一個特定事件中呼叫BackgroundWorker的CancelAsync方法﹝例如寫在某一個Button物件的Click事件中﹞。

    (3)
    在DoWork事件的程序中必要的位置建立檢查CancellationPending 屬性值的程序
    ﹝當BackgroundWorker的CancelAsync方法被呼叫後,這個值將會成為True﹞,
    並在此一屬性值轉為True時將傳入DoWork事件的DoWorkEventArgs的Cancel屬性值設為True。
     
    (4)
    在BackgroundWorker的RunWorkerCompleted事件中檢查傳入此事件的RunWorkerCompletedEventArgs的Cancelled屬性值
    ﹝這個值就是由(3)所描述的System.ComponentModel.DoWorkEventArgs的Cancel屬性值傳進來的﹞,
    當這個值為Fasle則呼叫RunWorkerAsync方法重新引發DoWork事件重新開始一個新的背景執行緒;若為True則不再繼續。

    詳情請參閱  http://www.dotblogs.com.tw/billchung/archive/2009/05/30/8597.aspx
    會寫程式不代表會通靈
    2009年6月10日 上午 11:23
    版主