gfritz74
Goto Top

WPF Timer zählt doppelt runter

Guten Morgen zusammen,

ich habe mit ein WPF Form mit einem Timer gebaut.
Klappt auch soweit wunderbar.
Wenn ich das Form einmal aufrufe läuft alles richtig.
Aber wenn ich das Form ein 2tes mal aufrufe zählt der nicht eine Sekunde runter sonder 2.
Beim 3ten mal sind es dann 3.

Keine Ahnung wodran das liegt.
Hier mal der Code
#Alle Variablen aus vorherigen Ausführungen entfernen
Remove-Variable * -ErrorAction SilentlyContinue
$ErrorActionPreference = "Continue"  



#Assemblys laden
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Presentationframework

# Bildschirmgröße ermitteln$Screen
$Screen = ([System.Windows.Forms.Screen]::AllScreens |? Primary).Bounds.Size

#####################
# Script Funktionen #
#####################
Function Close-Form {
    #$timer.Enabled = $false
    #$timer.Dispose()
   $TimerForm.close
   }



#Functionen für TimerForm
Function Format-TimeSpan
{
    PARAM (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [TimeSpan]$TimeSpan
    )
    $hours = $TimeSpan.Hours.ToString("00")  
    $minutes = $TimeSpan.Minutes.ToString("\:00")  
    $seconds = $TimeSpan.Seconds.ToString("\:00")  
}


#Function TimerForm
Function TimerForm{


[xml]$Timer_Form = @"  

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  

                Title="Computer Reboot Notifcation" Icon="C:\logos\Warnung.png" Height="446" Width="725" ResizeMode="NoResize" Topmost="True" WindowStartupLocation="CenterScreen" Background="White" FontFamily="Arial Black" ShowInTaskbar="False" SizeToContent="WidthAndHeight">  
    <Grid Margin="15,15,15,15">  
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Content="Nach der Installation von Sicherheitsupdates ist ein Neustart des Computers zwingend erforderlich.&#xD;&#xA;Dieser ist auch nach mehreren Hinweisen nicht erfolgt.&#xD;&#xA;Zur Wahrung der Sicherheitsniveuas muss dieser Heute erfolgen.&#xD;&#xA;Sie haben nun noch einmalig die Möglichkeit den Restart zu verschieben.&#xD;&#xA;Ansonsten sichern Sie Ihre Daten, schließen die geöffneten Anwendungen &#xD;&#xA;und klicken anschließend auf den Button Neustart.&#xD;&#xA;&#xD;&#xA;WARNUNG: Der Rechner wird nach Ablauf der Zeit neu gestartet!&#xD;&#xA;&#xD;&#xA;Vielen Dank ihr IT-Services-Team" FontFamily="Arial" FontWeight="Bold" FontSize="14" Padding="10,10,10,10" RenderTransformOrigin="0.5,0.5" Margin="0,0,0,200">  
            <Label.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform AngleX="0"/>  
                    <RotateTransform/>
                    <TranslateTransform X="0"/>  
                </TransformGroup>
            </Label.RenderTransform>
        </Label>
        <TextBox x:Name="TimeBox" BorderThickness="0" HorizontalAlignment="Left" Margin="10,200,0,0" IsReadOnly = "True" TextWrapping="Wrap" VerticalAlignment="Top" Width="217" Height="60" FontFamily="Arial" FontSize="40" FontWeight="Bold" Foreground="Red"/>  
        <Grid Margin="10,270,10,0">  
            <Button x:Name="B_Reboot" Content="Neustart" HorizontalAlignment="Left" Margin="0,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.393,0.489" Width="120" Height="50" FontFamily="Arial" FontWeight="Bold"/>  
            <Button x:Name="B_1Hour" Content="1 Stunde&#xD;&#xA;verzögern" HorizontalAlignment="Left" Margin="140,20,0,0" VerticalAlignment="Top" Width="120" Height="50" FontFamily="Arial" FontWeight="Bold"/>  
            <Button x:Name="B_4hour" Content="4 Stunden&#xD;&#xA;verzögern" HorizontalAlignment="Left" Margin="280,20,0,0" VerticalAlignment="Top" Width="120" Height="50" FontFamily="Arial" FontWeight="Bold"/>  
            <Image HorizontalAlignment="Left" Height="99" VerticalAlignment="Top" Width="120" Margin="545,0,0,0" Source="C:\logos\IS_Logo.bmp" Stretch="Fill"/>  
        </Grid>
        <Grid Margin="-1030,0,1030,133"/>  
    </Grid>

</Window>
"@  

#Create a form
$XMLReader = (New-Object System.Xml.XmlNodeReader $Timer_Form)
$TimerForm = [Windows.Markup.XamlReader]::Load($XMLReader)

#Connect to Controls
$TimeBox = $TimerForm.FindName('TimeBox')  

#Event handlers             
$TimerForm.Add_SourceInitialized({
    # Before the window's even displayed ...              
    # We'll create a timer              
    #$script:seconds =([new.timespan]0).Add('4:0:0')  # 2 minutes  
    $script:seconds = new-timespan -hour 4 -minute 0 -Seconds 0
    $script:timer = new-object System.Windows.Threading.DispatcherTimer
    # Which fire 1 time each second
    $timer.Interval = [TimeSpan]'0:0:1.0'  
    # And will invoke the $updateBlock          
    $timer.Add_Tick.Invoke($UpDateBlock)
    # Now start the timer running            
    $timer.Start()
    if ($timer.IsEnabled -eq $false) {
        write-warning "Timer didn't start"  
    }
})


$UpDateBlock = ({
    $script:seconds= $script:seconds.Subtract('0:0:1')  
    #$timer.Interval = [TimeSpan]'0:0:1.0'  
    #$Timebox.Text= $TimeSpan.FromTicks(14400).ToString('hh\:mm\:ss')  
    $Timebox.Text=$seconds.ToString('hh\:mm\:ss')  
    $timer.Start
    
    if($seconds -eq 0) {  Close-Form  }
})

#remove-Variable $UpDateBlock

#$LabelTimer = $TimerForm.FindName('LabelTimer')  


$B_Reboot = $TimerForm.FindName('B_Reboot')  
$B_Reboot.add_click({$Timer.Stop()})

#1Hour Button
$B_1Hour = $TimerForm.FindName('B_1Hour')  
$LabelTimer = $TimerForm.FindName('LabelTimer')  
$B_1Hour.add_click({ 
      
      $Timer.Stop()
      $script:totaltime =([timespan]0).Add('0:0:10')  # 2 minutes  
      $script:timer = new-object System.Windows.Threading.DispatcherTimer
      $timer.Interval = [TimeSpan]'0:0:1.0'  
      $timer.Add_Tick.Invoke($UpDateBlock)            
      $timer.Start()
      $B_1Hour.IsEnabled = $false
      $B_4Hour.IsEnabled = $false
})

#4Hour Button
$B_4hour = $TimerForm.FindName('B_4hour')  
$B_4hour.add_click({
      $Timer.Stop()
      $script:totaltime =([timespan]0).Add('0:0:10')  # 2 minutes  
      $script:timer = new-object System.Windows.Threading.DispatcherTimer
      $timer.Interval = [TimeSpan]'0:0:1.0'  
      $timer.Add_Tick.Invoke($UpDateBlock)            
      $timer.Start()
      $B_1Hour.IsEnabled = $false
      $B_4Hour.IsEnabled = $false
      })


# Position auf Bildschirm begrenzen

$timerform.add_LocationChanged({
    if ($this.Left -lt 0){$this.Left = 0}
    if ($this.Top -lt 0){$this.Top = 0}
    if ($this.Top + $this.Height -gt $Screen.Height){$this.Top = $Screen.Height - $this.Height}
    if ($this.Left + $this.Width -gt $Screen.Width){$this.Left = $Screen.Width - $this.Width}
})
$TimerForm.ShowDialog()
}

#$TimerForm.ShowDialog()

Het vielleicht eine ne Idee?

Beste Grüße
gfritz74

Content-Key: 1341982656

Url: https://administrator.de/contentid/1341982656

Printed on: April 24, 2024 at 01:04 o'clock