许多经常执行的操作可能需要很长的执行时间。例如:
*
图像下载
*
Web 服务调用
*
文件下载和上载(包括点对点应用程序)
*
复杂的本地计算
*
数据库事务
*
本地磁盘访问(相对于内存访问来说其速度很慢)
例子里是微软本身的例子和我的例子,一个窗体两个 BackgroundWorker,两个可以一起运行啊
第一个是求斐波纳契数
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker%28VS.80%29.aspx
第二个是不断地向richtextbox里添加行,类似于日志,完成一个后在里面显示一下。
微软的东西都封装了哎。例子外有几个问题:
(1)“您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。而应该通过 ProgressChanged 和 RunWorkerCompleted 事件与用户界面进行通信。”
但是测试发现,放在ProgressChangedUI还是没有响应的。所以只能放在 DoWork里
(2) 如果出现Cross-thread operation not valid: Control 'RichTextBox1' accessed from a thread other than the thread it was created on.
在窗体的构造方法里加上这个即可。
Control.CheckForIllegalCrossThreadCalls = False
.vb
Imports System.ComponentModel
Public Class TestBackGroundWork
Private lStrValue = String.Empty
Private numberToCompute As Integer = 0
Private highestPercentageReached As Integer = 0
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
Me.BackgroundWorker1.WorkerSupportsCancellation = True
' Add any initialization after the InitializeComponent() call.
'Control.CheckForIllegalCrossThreadCalls = False
End Sub
msdn 例子
不停的向RichTextBox添加行
End Class
Public Class TestBackGroundWork
Private lStrValue = String.Empty
Private numberToCompute As Integer = 0
Private highestPercentageReached As Integer = 0
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
Me.BackgroundWorker1.WorkerSupportsCancellation = True
' Add any initialization after the InitializeComponent() call.
'Control.CheckForIllegalCrossThreadCalls = False
End Sub
msdn 例子
不停的向RichTextBox添加行
End Class
.vb design
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class TestBackGroundWork
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
Me.Button3 = New System.Windows.Forms.Button
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.BackgroundWorker2 = New System.ComponentModel.BackgroundWorker
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(48, 130)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Start"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(166, 130)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Cancel"
Me.Button2.UseVisualStyleBackColor = True
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32, 23)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(130, 20)
Me.TextBox1.TabIndex = 2
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(204, 23)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(241, 20)
Me.TextBox2.TabIndex = 2
'
'BackgroundWorker1
'
Me.BackgroundWorker1.WorkerReportsProgress = True
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(32, 80)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(413, 23)
Me.ProgressBar1.TabIndex = 3
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(277, 130)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 23)
Me.Button3.TabIndex = 4
Me.Button3.Text = "AppandText"
Me.Button3.UseVisualStyleBackColor = True
'
'RichTextBox1
'
Me.RichTextBox1.AutoWordSelection = True
Me.RichTextBox1.Location = New System.Drawing.Point(48, 159)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(819, 262)
Me.RichTextBox1.TabIndex = 5
Me.RichTextBox1.Text = ""
'
'BackgroundWorker2
'
Me.BackgroundWorker2.WorkerReportsProgress = True
Me.BackgroundWorker2.WorkerSupportsCancellation = True
'
'TestBackGroundWork
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(949, 446)
Me.Controls.Add(Me.RichTextBox1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "TestBackGroundWork"
Me.Text = "TestBackGroundWork"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Private WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents BackgroundWorker2 As System.ComponentModel.BackgroundWorker
End Class
Partial Class TestBackGroundWork
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
Me.Button3 = New System.Windows.Forms.Button
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.BackgroundWorker2 = New System.ComponentModel.BackgroundWorker
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(48, 130)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Start"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(166, 130)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Cancel"
Me.Button2.UseVisualStyleBackColor = True
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32, 23)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(130, 20)
Me.TextBox1.TabIndex = 2
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(204, 23)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(241, 20)
Me.TextBox2.TabIndex = 2
'
'BackgroundWorker1
'
Me.BackgroundWorker1.WorkerReportsProgress = True
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(32, 80)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(413, 23)
Me.ProgressBar1.TabIndex = 3
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(277, 130)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 23)
Me.Button3.TabIndex = 4
Me.Button3.Text = "AppandText"
Me.Button3.UseVisualStyleBackColor = True
'
'RichTextBox1
'
Me.RichTextBox1.AutoWordSelection = True
Me.RichTextBox1.Location = New System.Drawing.Point(48, 159)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(819, 262)
Me.RichTextBox1.TabIndex = 5
Me.RichTextBox1.Text = ""
'
'BackgroundWorker2
'
Me.BackgroundWorker2.WorkerReportsProgress = True
Me.BackgroundWorker2.WorkerSupportsCancellation = True
'
'TestBackGroundWork
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(949, 446)
Me.Controls.Add(Me.RichTextBox1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "TestBackGroundWork"
Me.Text = "TestBackGroundWork"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Private WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents BackgroundWorker2 As System.ComponentModel.BackgroundWorker
End Class