none
委托事件 RRS feed

  • 问题

  •  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

            m_CountTo = 30
            BackgroundWorker1.RunWorkerAsync()

        End Sub


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

            TextBox1.Text = lines(i).ToString  ///这里出现错误
            If textbox1.text = "0" Then
               label1.text="OK"
            Else
                i += 1
            End If

            System.Threading.Thread.Sleep(1000)
            BackgroundWorker1.ReportProgress(CInt((i / m_CountTo) * 100))


        End Sub

        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            Me.ProgressBar1.Value = e.ProgressPercentage

        End Sub

    出现Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.我有查过,大部分都说要用·委托方法来解决。。如下

     Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] AsString)

            ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.

            ' If these threads are different, it returns true.

            If [Label].InvokeRequired Then

                Dim MyDelegate As New SetLabelText_Delegate(AddressOfSetLabelText_ThreadSafe)

                Me.Invoke(MyDelegate, New Object() {[Label], [text]})

            Else

                [Label].Text = [text]

            End If

        End Sub


     但不明白什么意思,可以解释吗。还有我不知道要如何修改来配合我的代码。。大大帮帮我。。。

    2012年3月1日 4:06

答案

全部回复

  • 请参考:
    How To Update Controls Using BackgroundWorker in VB.NET

    Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    	Me.ProgressBar1.Value = e.ProgressPercentage
    	TextBox1.Text = e.UserState
    	If TextBox1.Text = "0" Then
    		Label1.Text = "OK"
    	Else
    		i += 1
    	End If
    End Sub
    
    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    	'TextBox1.Text = lines(i).ToString '改成以下用變數去接
    	Dim ListText As String = lines(i).ToString
    	'移到BackgroundWorker1_ProgressChanged
    	
    	'If TextBox1.Text = "0" Then
    	'    Label1.Text = "OK"
    	'Else
    	'    i += 1
    	'End If
    
    	System.Threading.Thread.Sleep(1000)
    	'BackgroundWorker1.ReportProgress(CInt((i / m_CountTo) * 100))
    	BackgroundWorker1.ReportProgress(CInt((i / m_CountTo) * 100), ListText)
    End Sub



    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/


    2012年3月1日 4:36
  • Hi ckjason,

      这边有个不错的例子适合你了解如何使用Backgroundworker去进行进度控制和计算。

      Do time-consuming operations in Windows Forms (VBWinFormTimeConsumingOpr)

      http://code.msdn.microsoft.com/VBWinFormTimeConsumingOpr-c3d7698d 


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • 已标记为答案 ckjason 2012年3月6日 3:05
    2012年3月1日 7:34