Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
PublicClass Form2
Inherits Form
' This delegate enables asynchronous calls for setting
' the text property on a TextBox control.
DelegateSub SetTextCallback(ByVal [text] AsString)
' This thread is used to demonstrate both thread-safe and
' unsafe ways to call a Windows Forms control.
Private demoThread As Thread = Nothing
' This BackgroundWorker is used to demonstrate the
' preferred way of performing asynchronous operations.
PrivateWithEvents backgroundWorker1 As BackgroundWorker
Private textBox1 As TextBox
PrivateWithEvents setTextUnsafeBtn As Button
PrivateWithEvents setTextSafeBtn As Button
PrivateWithEvents setTextBackgroundWorkerBtn As Button
Private components As System.ComponentModel.IContainer = Nothing
PublicSubNew()
InitializeComponent()
EndSub
ProtectedOverridesSub Dispose(ByVal disposing AsBoolean)
If disposing AndAlso (components IsNotNothing) Then
components.Dispose()
EndIf
MyBase.Dispose(disposing)
EndSub
' This event handler creates a thread that calls a
' Windows Forms control in an unsafe way.
PrivateSub setTextUnsafeBtn_Click(ByVal sender AsObject, ByVal e As EventArgs) Handles setTextUnsafeBtn.Click
Me.demoThread = New Thread(New ThreadStart(AddressOfMe.ThreadProcUnsafe))
Me.demoThread.Start()
EndSub
' This method is executed on the worker thread and makes
' an unsafe call on the TextBox control.
PrivateSub ThreadProcUnsafe()
Me.textBox1.Text = "This text was set unsafely."
EndSub
' This event handler creates a thread that calls a
' Windows Forms control in a thread-safe way.
PrivateSub setTextSafeBtn_Click(ByVal sender AsObject, ByVal e As EventArgs) Handles setTextSafeBtn.Click
Me.demoThread = New Thread(New ThreadStart(AddressOfMe.ThreadProcSafe))
Me.demoThread.Start()
EndSub
' This method is executed on the worker thread and makes
' a thread-safe call on the TextBox control.
PrivateSub ThreadProcSafe()
Me.SetText("This text was set safely.")
EndSub
' This method demonstrates a pattern for making thread-safe
' calls on a Windows Forms control.
'
' If the calling thread is different from the thread that
' created the TextBox control, this method creates a
' SetTextCallback and calls itself asynchronously using the
' Invoke method.
'
' If the calling thread is the same as the thread that created
' the TextBox control, the Text property is set directly.
PrivateSub SetText(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.
IfMe.textBox1.InvokeRequired Then
Dim d AsNew SetTextCallback(AddressOf SetText)
Me.Invoke(d, NewObject() {[text]})
Else
Me.textBox1.Text = [text]
EndIf
EndSub
' This event handler starts the form's
' BackgroundWorker by calling RunWorkerAsync.
'
' The Text property of the TextBox control is set
' when the BackgroundWorker raises the RunWorkerCompleted
' event.
PrivateSub setTextBackgroundWorkerBtn_Click(ByVal sender AsObject, ByVal e As EventArgs) Handles setTextBackgroundWorkerBtn.Click
Me.backgroundWorker1.RunWorkerAsync()
EndSub
' This event handler sets the Text property of the TextBox
' control. It is called on the thread that created the
' TextBox control, so the call is thread-safe.
'
' BackgroundWorker is the preferred way to perform asynchronous
' operations.
PrivateSub backgroundWorker1_RunWorkerCompleted(ByVal sender AsObject, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
Me.textBox1.Text = _
"This text was set safely by BackgroundWorker."
EndSub
#Region"Windows Form Designer generated code"
PrivateSub InitializeComponent()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.setTextUnsafeBtn = New System.Windows.Forms.Button()
Me.setTextSafeBtn = New System.Windows.Forms.Button()
Me.setTextBackgroundWorkerBtn = New System.Windows.Forms.Button()
Me.backgroundWorker1 = New System.ComponentModel.BackgroundWorker()
Me.SuspendLayout()
'
' textBox1
'
Me.textBox1.Location = New System.Drawing.Point(12, 12)
Me.textBox1.Name = "textBox1"
Me.textBox1.Size = New System.Drawing.Size(240, 20)
Me.textBox1.TabIndex = 0
'
' setTextUnsafeBtn
'
Me.setTextUnsafeBtn.Location = New System.Drawing.Point(15, 55)
Me.setTextUnsafeBtn.Name = "setTextUnsafeBtn"
Me.setTextUnsafeBtn.TabIndex = 1
Me.setTextUnsafeBtn.Text = "Unsafe Call"
'
' setTextSafeBtn
'
Me.setTextSafeBtn.Location = New System.Drawing.Point(96, 55)
Me.setTextSafeBtn.Name = "setTextSafeBtn"
Me.setTextSafeBtn.TabIndex = 2
Me.setTextSafeBtn.Text = "Safe Call"
'
' setTextBackgroundWorkerBtn
'
Me.setTextBackgroundWorkerBtn.Location = New System.Drawing.Point(177, 55)
Me.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn"
Me.setTextBackgroundWorkerBtn.TabIndex = 3
Me.setTextBackgroundWorkerBtn.Text = "Safe BW Call"
'
' backgroundWorker1
'
'
' Form1
'
Me.ClientSize = New System.Drawing.Size(268, 96)
Me.Controls.Add(setTextBackgroundWorkerBtn)
Me.Controls.Add(setTextSafeBtn)
Me.Controls.Add(setTextUnsafeBtn)
Me.Controls.Add(textBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
EndSub'InitializeComponent
#EndRegion
<STAThread()> _
SharedSub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
EndSub
EndClass
منبع