Hallo Michael,
Post by Michael KolowiczIch wollte jetzt gerne A ausführen lassen, B und C anhalten, wenn A
fertig ist, B (jetzt noch 3 Minuten) und C (noch 118 Minuten)
weiterlaufen lassen
Ich habe ein kleines Beispielprojekt erstellt.
Erstelle eine leere Form und füge einen Timer (Timer1) und den
nachfolgenden Code ein. Pass bitte bei den Zeilenumbrüchen auf.
'Begin
#Region "Constants and Variables for the solution"
Private Const iDATE_INTERVAL_A = 6 'Seconds for TimerA
Private Const iDATE_INTERVAL_B = 30 'Seconds for TimerB
Private Const iDATE_INTERVAL_C = 42 'Seconds for TimerC
Private DateA As Date 'Date for next tick of TimerA
Private DateB As Date 'Date for next tick of TimerB
Private DateC As Date 'Date for next tick of TimerC
Private iCurrentSecondsA As Integer 'CurrentNumber of seconds of TimerA
Private iCurrentSecondsB As Integer 'CurrentNumber of seconds of TimerB
Private iCurrentSecondsC As Integer 'CurrentNumber of seconds of TimerC
#End Region
#Region "Variables for the demonstration"
Private PicA As PictureBox
Private PicB As PictureBox
Private PicC As PictureBox
Private GrpA As GroupBox
Private GrpB As GroupBox
Private GrpC As GroupBox
Private dStart As Date
Private Const sDATE_FORMAT = "dd. MMM yyyy HH:mm:ss"
#End Region
#Region "Subs and functions for the solution"
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Time()
CheckTimes()
End Sub
Private Sub CheckTimes()
'This routine checks if one or more timers reached the time to fire.
Dim dNow As Date = Now
Dim iDiffA As Integer
Dim iDiffB As Integer
Dim iDiffC As Integer
'Check if it is 'Fire' time...
iDiffA = DateDiff(DateInterval.Second, dNow, DateA)
iDiffB = DateDiff(DateInterval.Second, dNow, DateB)
iDiffC = DateDiff(DateInterval.Second, dNow, DateC)
If iDiffA < 1 OrElse iDiffB < 1 OrElse iDiffC < 1 Then
Timer1.Enabled = False
If iDiffA < 1 Then
TickA()
End If
If iDiffB < 1 Then
TickB()
End If
If iDiffC < 1 Then
TickC()
End If
CalcRemainingSeconds(dNow)
CalcNewTimesAndEnableTimer()
End If
End Sub
Private Sub CalcRemainingSeconds(ByVal dNow As Date)
'This routine calculates the remaining seconds.
iCurrentSecondsA = CalcRemainingSeconds(dNow, DateA)
iCurrentSecondsB = CalcRemainingSeconds(dNow, DateB)
iCurrentSecondsC = CalcRemainingSeconds(dNow, DateC)
End Sub
Private Function CalcRemainingSeconds(ByVal dNow As Date, ByVal dTimer
As Date) As Integer
'This routine calculates the remaining seconds for a specific timer.
Dim retVal As Integer
retVal = DateDiff(DateInterval.Second, dNow, dTimer)
If retVal < 0 Then
retVal = 0
End If
Return retVal
End Function
Private Sub CalcNewTimesAndEnableTimer()
'This routine calculates the new time for the timers next fire events.
Dim dNow As Date = Now
DateA = CalcNewTime(dNow, iCurrentSecondsA, iDATE_INTERVAL_A)
DateB = CalcNewTime(dNow, iCurrentSecondsB, iDATE_INTERVAL_B)
DateC = CalcNewTime(dNow, iCurrentSecondsC, iDATE_INTERVAL_C)
Timer1.Enabled = True
End Sub
Private Function CalcNewTime(ByVal dNow As Date, ByVal iSecondsToGo As
Integer, ByVal iDefaultSeconds As Integer) As Date
'This routine calculates the new time for a specific timer.
Dim iSeconds As Integer
Dim retVal As Date
If iSecondsToGo < 1 Then
iSeconds = iDefaultSeconds
Else
iSeconds = iSecondsToGo
End If
retVal = DateAdd(DateInterval.Second, iSeconds, dNow)
Return retVal
End Function
Private Sub TickA()
'Fire event for TimerA.
DoSomething(PicA, GrpA) 'Here is your routine
End Sub
Private Sub TickB()
'Fire event for GimerB.
DoSomething(PicB, GrpB) 'Here is your routine
End Sub
Private Sub TickC()
'Fire event for TimerC.
DoSomething(PicC, GrpC) 'Here is your routine
End Sub
#End Region
#Region "Subs and functions for the demonstration"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dStart = Now
PrepareForm()
CalcNewTimesAndEnableTimer()
End Sub
Private Sub PrepareForm()
Me.Width = 630
Me.Height = 280
Time()
PrepareForm("A", 5, 5, PicA, GrpA)
PrepareForm("B", 210, 5, PicB, GrpB)
PrepareForm("C", 420, 5, PicC, GrpC)
End Sub
Private Sub Time()
Dim sStart As String = dStart.ToString(sDATE_FORMAT)
Dim sNow As String = Now.ToString(sDATE_FORMAT)
Me.Text = "Start: " & sStart & " / Now: " & sNow
End Sub
Private Sub PrepareForm(ByVal Name As String, ByVal iLeft As Integer,
ByVal iTop As Integer, ByRef PicBox As PictureBox, ByRef GrpBox As GroupBox)
Dim grp As New GroupBox
Dim Pic As New PictureBox
grp.Name = "Grp" & Name
grp.Width = 200
grp.Height = 220
grp.Text = "Timer " & Name
grp.Tag = grp.Text & ": ("
grp.Left = iLeft
grp.Top = iTop
grp.Visible = True
Pic.Name = "Pic" & Name
Pic.Width = 190
Pic.Height = 190
Pic.Left = 5
Pic.Top = 20
Pic.Visible = True
Me.Controls.Add(grp)
grp.Controls.Add(Pic)
PicBox = Pic
GrpBox = grp
End Sub
Private Sub DoSomething(ByRef Pic As PictureBox, ByRef grp As GroupBox)
Dim rn As New Random()
Dim r As Integer = rn.Next(0, 255)
Dim g As Integer = rn.Next(0, 255)
Dim b As Integer = rn.Next(0, 255)
Dim bmp As New Bitmap(Pic.Width, Pic.Height)
Dim clr As Color = Color.FromArgb(255, r, g, b)
Dim Pn As New Pen(clr)
Dim gr As Graphics = Graphics.FromImage(bmp)
Dim rct As New Rectangle(0, 0, bmp.Width, bmp.Height)
gr.DrawRectangle(Pn, rct)
gr.FillRectangle(Pn.Brush, rct)
gr.Dispose()
Pic.Image = Image.FromHbitmap(bmp.GetHbitmap)
grp.Text = grp.Tag & Now.ToString(sDATE_FORMAT) & ")"
End Sub
#End Region
'End
Beste Grüße,
Martin