Hallöchen!
Ich als VB.Net Neuling habe da ein kleines Problem, hoffe ich finde hier ein paar anregende Tipps um dieses zu beheben.
Hab mir einen kleinen Counter zusammengebastelt welcher die verbleibende Zeit in einer Textbox ausgibt. Zusätzlich hat meine Form auch noch ein StatusStrip mit Progressbar und Label. Nun kommt es immer wieder vor das mir die Windows Form einfriert und das völlig willkürlich. Egal ob das Programm im Vordergrund oder im Hintergrund läuft, nebenbei etwas anderes am PC gearbeitet wird oder minimiert oder maximiert läuft. Das kann passieren, Sekunden nachdem das Programm gestartet wurde oder auch erst nach 2 Tagen, wenn das Programm permanent durchlief.
Habe so die Vermutung das hier irgendetwas nicht mit der Threadsynchronisierung vom Thread der Windows Form und meinen erstellten Thread hinaut, sicher bin ich mir aber nicht.
Hoffe jemanden fällt bei meinen Code etwas auf.
mfg
Windows Form:
Imports System.Threading
Public Delegate Sub SetTextCallback(ByVal text As String) 'Delegate für
' Textboxen
Public Delegate Sub addProgress(ByVal cnt As Integer) 'Delegate für Progress Bar
Public Class gui
Private Shared _txtBoxStat As RichTextBox
Private Shared _txtBoxLv As RichTextBox
Private Shared _txtBoxOp As RichTextBox
Private Shared _statusStrip As StatusStrip
Private Shared _status As ToolStripStatusLabel
Private Shared _progBar As ToolStripProgressBar
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnSt.Click
_txtBoxStat = Me.txtStat 'Textbox in welche Zeit geschrieben wird
_txtBoxLv = Me.txtLv 'Weitere Textbox in Form
_txtBoxOp = Me.txtOp 'Weitere Textbox in Form
_statusStrip = Me.StatusStrip1 'Statusstrip
_status = Me.sta 'Statusstrip Label
_progBar = Me.prg 'Statusstrip ProgressBar
Dim thStart As Thread 'Mein Thread
thStart = New Threading.Thread(New ThreadStart(AddressOf start))
thStart.Start()
End Sub
Friend Sub start()
While 1 'Aus Testzwecken verwende ich hier eine Endlosschleife
Dim t As New timer
Me.insertProgress(-1000) 'ProgressBar auf 0 setzen
t.cntThread(1000) 'Counter aufrufen mit 1000 Sekunden
End While
End Sub
'In Progressbar schreiben
Public Sub insertProgress(ByVal cnt As Integer)
If _statusStrip.InvokeRequired Then
Dim d As New addProgress(AddressOf insertProgress)
_statusStrip.Invoke(d, New Object() {cnt})
Else
_progBar.Increment(cnt)
End If
End Sub
'In Statusstriplabel Schreiben
Public Sub SetTextStrip(ByVal text As String)
If _statusStrip.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetTextStrip)
_statusStrip.Invoke(d, text)
Else
_status.Text = text
End If
End Sub
'In Textbox Status schreiben
Friend Sub setTextStat(ByVal text As String)
If _txtBoxStat.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf setTextStat)
_txtBoxStat.Invoke(d, text)
Else
_txtBoxStat.Text = text
End If
End Sub
End Class timer:
Option Explicit On
Imports System.Threading
Public Class timer
Private Shared stateTimer As System.Threading.Timer
Private Shared endTime As DateTime 'Zeitpunkt des Endes
Private Shared timeLeft As TimeSpan 'Verbelibende Zeit
'Timer Definition
Public Sub cntThread(ByVal _duration As Integer) '_duration = Zeit in
' Sekunden
Using autoEvent As New AutoResetEvent(False)
endTime = DateTime.Now.Add(TimeSpan.FromSeconds(_duration)) _
'Endzeit berechnen
Dim timerDelegate As TimerCallback = AddressOf CheckStatus
stateTimer = New System.Threading.Timer(timerDelegate, autoEvent, _
0, 1000)
autoEvent.WaitOne()
stateTimer.Dispose()
End Using
End Sub
'Counter durchlaufen
Friend Sub CheckStatus(ByVal stateInfo As Object)
Using autoEvent As AutoResetEvent = DirectCast(stateInfo, _
AutoResetEvent)
If DateTime.Now >= endTime Then 'Wenn Zeit abgelaufen
autoEvent.Set()
autoEvent.Close()
Exit Sub
Else
timeLeft = endTime.Subtract(DateTime.Now) 'Berechnung der
' verbleibenden Zeit
gui.setTextStat("Continue in: " + Format(timeLeft.Hours, "00") _
+ ":" + _ Format(timeLeft.Minutes, "00") + _
":" + Format(timeLeft.Seconds, "00")) 'in Textbox Status
' schreiben
End If
End Using
End Sub
End Class -----------------------------------------------------
WIN7, 64bit
4GB
Core 2 Duo
Nvidia GForce G103M Cuda
Visual Studio 2010 Ultimate |