Re: Fortschrittsbalken für Funktion

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Mathias Kant wrote:
....
Er gibt leider ein Fehler aus: Ungültiger threadübergreifender
Vorgang.

Mathias,
besser ist es, in der Routine, die die Callback-Roitine aufruft, den
thread-übergriefenden Zugriff mittels Invoke auszuführen, z.B. so:

Private pb As New ProgressBar
Private endflag As Boolean
Delegate Sub ShowProgress()

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Prepare stop ProgressBar with any KeyDown
With Me
.KeyPreview = True
AddHandler .KeyDown, AddressOf StopPd
End With
'
With pb
.Dock = DockStyle.Top
Me.Controls.Add(pb)
End With
' Create a delegate that points to it.
Dim asyncFill As New ShowProgress(AddressOf ExecProgress)
' Open it asynchronously - pass the delegate as the cookie.
Dim ar As IAsyncResult = _
asyncFill.BeginInvoke(AddressOf ShowComplete, asyncFill)
'...
' main process
'Form1.autom.automatKlass(Form1.ComboBox1.Text, Form1.xmldoc).OuterXml
'...
End Sub

Private Sub StopPd(ByVal sender As Object, ByVal e As KeyEventArgs)
endflag = True
End Sub

Private Sub IncrementValuePb()
With pb
If .InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf IncrementValuePb))
Else
If .Value = .Maximum Then
.Value = 0
Else
.Value += 1
End If
End If
End With
End Sub

' this method is called when the operation is completed
Private Sub ShowComplete(ByVal ar As IAsyncResult)
' retrieve a reference to the delegate.
Dim asyncFill As ShowProgress = CType(ar.AsyncState, ShowProgress)
Try
' Complete the operation.
asyncFill.EndInvoke(ar)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub

' Show ProgressBar and animate
Private Sub ExecProgress()
' reset End-Flag
endflag = False
' ProgressBar
Do Until endflag Or Me.IsDisposed
IncrementValuePb()
System.Threading.Thread.Sleep(50)
Loop
End Sub

Peter



.



Relevant Pages

  • =?iso-8859-1?Q?Re:_Linq_f=FCr_Suche_in_Liste_nutzen?=
    ... Private Delegate Sub myDelegate() ... Private Sub ToolStripTextBox1_TextChangedHandles ToolStripTextBox1.TextChanged ... Private Sub Search() ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Linq für Suche in Liste nutzen
    ... Lediglich bekomme ich ab und an, ... Private Delegate Sub myDelegate() ... Private Sub ToolStripTextBox1_TextChanged _ ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Threading
    ... The problem is that the m_oCallbackDelegate delegate is executed on the ... execution of m_oCallbackDelegate onto the UI thread. ... Private Class SideStep ... Friend Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ComPort-Problem
    ... Private WithEvents mobjComPort1 As System.IO.Ports.SerialPort ... Public Sub New() ... End With ... Private Delegate Sub objDelegateBufferLesen() ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Fortschrittsbalken für Funktion
    ... Private pb As New ProgressBar ... Private endflag As Boolean ... Delegate Sub ShowProgress() ... ' Create a delegate that points to it. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)