Also, es gibt zwei Methoden, um ein Thread mit Parametern zu starten. Du kannst eine Klasse schreiben, und iun dieser Klasse deine Thread Methode haben. Diese Methode kann dann Klassenvariablen benutzen. Die kannst du z.B. im Contructor belegen. Oder du kannst jede Methode mit einem Delegaten starten. Jeder Delagt hat die Methoden BeginInvoke() und EndInvoke(), die vom Compiler generiert werden. Mit diesen Methoden kannst du die Methode mit einem Threadpool Thread laufen lassen. Und das geht so (hier sind ziemlich allen Möglichkeiten eines Delegaten vorhanden):
Public Class AsynchDelegateForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
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.
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents StartButton As System.Windows.Forms.Button
Friend WithEvents CheckCompleteButton As System.Windows.Forms.Button
Friend WithEvents GetResultButton As System.Windows.Forms.Button
Friend WithEvents GetResultPollButton As System.Windows.Forms.Button
Friend WithEvents UseCallbackCheckBox As System.Windows.Forms.CheckBox
Friend WithEvents StartAndWaitButton As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.StartButton = New System.Windows.Forms.Button
Me.CheckCompleteButton = New System.Windows.Forms.Button
Me.StatusBar1 = New System.Windows.Forms.StatusBar
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.GetResultButton = New System.Windows.Forms.Button
Me.GetResultPollButton = New System.Windows.Forms.Button
Me.UseCallbackCheckBox = New System.Windows.Forms.CheckBox
Me.StartAndWaitButton = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'StartButton
'
Me.StartButton.Location = New System.Drawing.Point(24, 88)
Me.StartButton.Name = "StartButton"
Me.StartButton.Size = New System.Drawing.Size(80, 40)
Me.StartButton.TabIndex = 0
Me.StartButton.Text = "Start"
'
'CheckCompleteButton
'
Me.CheckCompleteButton.Location = New System.Drawing.Point(24, 184)
Me.CheckCompleteButton.Name = "CheckCompleteButton"
Me.CheckCompleteButton.Size = New System.Drawing.Size(80, 40)
Me.CheckCompleteButton.TabIndex = 1
Me.CheckCompleteButton.Text = "Check"
'
'StatusBar1
'
Me.StatusBar1.Location = New System.Drawing.Point(0, 245)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(536, 16)
Me.StatusBar1.TabIndex = 2
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(104, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Time in seconds:"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(104, 20)
Me.TextBox1.TabIndex = 4
Me.TextBox1.Text = "TextBox1"
'
'GetResultButton
'
Me.GetResultButton.Location = New System.Drawing.Point(120, 88)
Me.GetResultButton.Name = "GetResultButton"
Me.GetResultButton.Size = New System.Drawing.Size(80, 40)
Me.GetResultButton.TabIndex = 5
Me.GetResultButton.Text = "Get result"
'
'GetResultPollButton
'
Me.GetResultPollButton.Location = New System.Drawing.Point(216, 88)
Me.GetResultPollButton.Name = "GetResultPollButton"
Me.GetResultPollButton.Size = New System.Drawing.Size(80, 40)
Me.GetResultPollButton.TabIndex = 6
Me.GetResultPollButton.Text = "Get result poll"
'
'UseCallbackCheckBox
'
Me.UseCallbackCheckBox.Checked = True
Me.UseCallbackCheckBox.CheckState = _
System.Windows.Forms.CheckState.Checked
Me.UseCallbackCheckBox.Location = New System.Drawing.Point(24, 56)
Me.UseCallbackCheckBox.Name = "UseCallbackCheckBox"
Me.UseCallbackCheckBox.Size = New System.Drawing.Size(112, 16)
Me.UseCallbackCheckBox.TabIndex = 7
Me.UseCallbackCheckBox.Text = "Use callback"
'
'StartAndWaitButton
'
Me.StartAndWaitButton.Location = New System.Drawing.Point(24, 136)
Me.StartAndWaitButton.Name = "StartAndWaitButton"
Me.StartAndWaitButton.Size = New System.Drawing.Size(80, 40)
Me.StartAndWaitButton.TabIndex = 8
Me.StartAndWaitButton.Text = "Start and Wait"
'
'AsynchDelegateForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(536, 261)
Me.Controls.Add(Me.StartAndWaitButton)
Me.Controls.Add(Me.UseCallbackCheckBox)
Me.Controls.Add(Me.GetResultPollButton)
Me.Controls.Add(Me.GetResultButton)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.StatusBar1)
Me.Controls.Add(Me.CheckCompleteButton)
Me.Controls.Add(Me.StartButton)
Me.Name = "AsynchDelegateForm"
Me.Text = "AsynchDelegateForm"
Me.ResumeLayout(False)
End Sub
#End Region
' Delegate for DoSomethingTakingALongTime() signature
Private Delegate Function DoSomethingTakingALongTimeDelegate(ByVal ssecs As _
Integer) As String
' Delegate for SetStatusText() signature
Private Delegate Sub SetStatusTextDelegate(ByVal t As String)
' Delegate object for asynch calls
Private exec As New DoSomethingTakingALongTimeDelegate(AddressOf _
DoSomethingTakingALongTime)
' Asynchronous Call Result
Private execRes As IAsyncResult
' Shows callback active (in case user changes checkbox)
Private cbactive As Boolean
' Shows waiting in DoEvents loop
Private waiting As Boolean
' The routine to be called asynchronously
Private Function DoSomethingTakingALongTime(ByVal secs As Integer) As String
System.Threading.Thread.Sleep(secs * 1000)
Return "Some value from DoSomethingTakingALongTime"
End Function
' The Asynchronous result callback routine
Private Sub DoSomethingTakingALongTimeCallBack(ByVal ar As IAsyncResult)
Dim resp As String = exec.EndInvoke(ar)
Me.Invoke(New SetStatusTextDelegate(AddressOf SetStatusText), New _
Object() {"Call completed: " + resp + " (asynch)"})
execRes = Nothing
cbactive = False
End Sub
' Start asynch call
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles StartButton.Click
If execRes Is Nothing Then
StatusBar1.Text = "Call started"
If UseCallbackCheckBox.Checked Then
cbactive = True
execRes = exec.BeginInvoke(CInt(TextBox1.Text), AddressOf _
DoSomethingTakingALongTimeCallBack, Nothing)
Else
execRes = exec.BeginInvoke(CInt(TextBox1.Text), Nothing, _
Nothing)
End If
Else
SetStatusText("Call is already active")
End If
End Sub
' Check completion
Private Sub CheckCompleteButton_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles CheckCompleteButton.Click
If execRes Is Nothing Then
StatusBar1.Text = "Call completed"
Else
StatusBar1.Text = "Call not completed"
End If
End Sub
' Wait for and get result
Private Sub GetResultButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles GetResultButton.Click
If checkstatus() Then Return
' Call will wait until asynch completiom
Dim resp As String = exec.EndInvoke(execRes)
StatusBar1.Text = "Call completed: " + resp
execRes = Nothing
End Sub
' Get result by polling until finished
Private Sub GetResultPollButton_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles GetResultPollButton.Click
If checkstatus() Then Return
' Stop strange form behaviour due to mouse capture
GetResultPollButton.Capture = False
' Wait in DoEvents loop until asynch completion
waiting = True
Do While Not execRes.IsCompleted
Application.DoEvents()
System.Threading.Thread.Sleep(50)
Loop
waiting = False
' Now result is present
Dim resp As String = exec.EndInvoke(execRes)
StatusBar1.Text = "Call completed: " + resp
execRes = Nothing
End Sub
' Start and wait
Private Sub StartAndWaitButton_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles StartAndWaitButton.Click
If execRes Is Nothing Then
If UseCallbackCheckBox.Checked Then
StatusBar1.Text = "Cannot perform start and wait with callback" & _
"option set"
Return
End If
StatusBar1.Text = "Call started"
execRes = exec.BeginInvoke(CInt(TextBox1.Text), Nothing, Nothing)
' Stop strange form behaviour due to mouse capture
StartAndWaitButton.Capture = False
' Wait in DoEvents loop until asynch completion
waiting = True
Do While Not execRes.IsCompleted
Application.DoEvents()
System.Threading.Thread.Sleep(50)
Loop
waiting = False
' Now result is present
Dim resp As String = exec.EndInvoke(execRes)
StatusBar1.Text = "Call completed: " + resp
execRes = Nothing
Else
SetStatusText("Call is already active")
End If
End Sub
' Check status for get result buttons
Private Function checkstatus() As Boolean
If waiting Or cbactive Then
StatusBar1.Text = "Application waiting for asynch result"
Return True
End If
If execRes Is Nothing Then
StatusBar1.Text = "No call active"
Return True
End If
End Function
' Routine to set statusbar text (for delegate)
Private Sub SetStatusText(ByVal t As String)
StatusBar1.Text = t
End Sub
' Init
Private Sub AsynchDelegateForm_Load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "15"
End Sub
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |