Hallo,
ich habe eine kleine GUI gebastelt.
Die GUI enthält einen Copy und einen Cancel Button.
Durch "Klick" auf den Button Copy sollen logischerweise Daten kopiert werden.
Durch "Klick" auf den Button Cancel, soll der Kopiervorgang ggf. abgebrochen werden können.
Jetzt ist es ja so, dass während des Kopiervorganges die GUI eingefroren ist und der Cancel-Button
gar nicht betätigt werden kann bzw. keine Funktion hat.
Ich habe durch recherchieren öfters den Hinweis auf Multithreading, Runspaces und Jobs gefunden, allerdings
sahen mir die Beispiele meistens so aus, als wenn ich sie für die Lösung meines Problems nicht wirklich
gebrauchen könnte bzw. wüsste ich nicht, wie ich es sinnvoll anwenden könnte.
Hat jemand vielleicht einen Beispielcode oder Codeschnipsel, der meine beschriebene Problematik löst bzw. ansehnlicher
veranschaulicht. Ich erwarte keinen fertigen Code, aber vielleicht einen Ansatz in die richtige Richtung. Bitte keine
Schlagwörter wie Runspaces, Jobs oder Multithreading, denn das hat mich bisher leider auch noch nicht weiter gebracht.
Vielen Dank im voraus!
Habe den Code mal mit angehängt:
clear # Load Assemblies [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null # Init Form $Form = New-Object System.Windows.Forms.Form $Form.width = 400 $Form.height = 600 $Form.Backcolor=“white“ $Form.Text = "Test" # Init ProgressBar $pbrTest = New-Object System.Windows.Forms.ProgressBar $pbrTest.Maximum = 100 $pbrTest.Minimum = 0 $pbrTest.Location = new-object System.Drawing.Size(10,10) $pbrTest.size = new-object System.Drawing.Size(200,50) $i = 0 $Form.Controls.Add($pbrTest) # Button $btnConfirm = new-object System.Windows.Forms.Button $btnConfirm.Location = new-object System.Drawing.Size(250,10) $btnConfirm.Size = new-object System.Drawing.Size(100,30) $btnConfirm.Text = "Start Progress" $Form.Controls.Add($btnConfirm) ### Cancel $ButtonCancel = New-Object System.Windows.Forms.Button $ButtonCancel.Location = New-Object System.Drawing.Size(250,50) $ButtonCancel.Size = New-Object System.Drawing.Size(100,30) $ButtonCancel.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Bold) $ButtonCancel.BackColor = [System.Drawing.Color]::Red $ButtonCancel.ForeColor = [System.Drawing.Color]::Yellow $ButtonCancel.Add_MouseHover({$ButtonCancel.backcolor = [System.Drawing.Color]::Green}) $ButtonCancel.Add_MouseLeave({$ButtonCancel.backcolor = [System.Drawing.Color]::Red}) $ButtonCancel.Text = "Cancel" $ButtonCancel.Add_Click({ Write-Host "BREAK !!!" }) $Form.Controls.Add($ButtonCancel) ### Output box $LabelOutput = New-Object System.Windows.Forms.Label $LabelOutput.Location = New-Object System.Drawing.Size(10,90) $LabelOutput.Size = New-Object System.Drawing.Size(80,25) $LabelOutput.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Bold) $LabelOutput.ForeColor = [System.Drawing.Color]::Green $LabelOutput.Text = "Result(s): " $Form.Controls.Add($LabelOutput) $outputBox = New-Object System.Windows.Forms.TextBox $outputBox.Location = New-Object System.Drawing.Size(10,115) $outputBox.Size = New-Object System.Drawing.Size(350,400) $outputBox.MultiLine = $True $outputBox.ScrollBars = "Vertical" $Form.Controls.Add($outputBox) ################################################################################ ### Statusbar ################################################################################ $statusBar1 = New-Object System.Windows.Forms.StatusBar $statusBar1.Name = "statusBar1" $statusBar1.Text = "Status: ... Waiting ..." $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 284 $System_Drawing_Size.Height = 22 $statusBar1.Size = $System_Drawing_Size $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 0 $System_Drawing_Point.Y = 240 $statusBar1.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Bold) $statusBar1.Location = $System_Drawing_Point $statusBar1.DataBindings.DefaultDataSourceUpdateMode = 0 $statusBar1.TabIndex = 1 $Form.Controls.Add($statusBar1) # Button Click Event to Run ProgressBar $dest_path = "C:\TEST1 $data_path = "C:\TEST2 $robo_log = "C:\TEST\robo.log" $top_dirs = Get-ChildItem -Path $data_path -Directory $top_dirs = $top_dirs.Name $btnConfirm.Add_Click({ $i=0 foreach ($folder in $top_dirs) { Write-Host "folder: $($folder)" ### calculate percentage $i++ $pbrTest.Value = ($i/$top_dirs.count)*100 ### update the progress bar Start-Sleep -m 1 $folder_content = (gci -recurse $data_path\$folder) # $folder_content = $folder_content.Name Write-Host "folder_content: $folder_content" robocopy $data_path\$folder $dest_path\$folder /s /ndl /njh /njs /np /ns /nc /mir /log:$robo_log $robo_log_content = Get-Content "$robo_log" foreach ($log_entry in $robo_log_content){ $outputBox.AppendText("$($log_entry) `r`n") } ### show folder_content in outputBox # $outputBox.AppendText("$($folder_content) `r`n") } })<# $btnConfirm.Add_Click({ While ($i -le 100) { Write-Host "i: $($i)" $pbrTest.Value = $i Start-Sleep -m 1 "VALLUE EQ" $i $i += 1 robocopy $data_path\$folder $dest_path\$folder /s /ndl /njh /njs /np /ns /nc /mir /Tee } }) #> # Show Form $Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog()