I created a winforms gui for my ps script.
By clicking on start button the script takes data from a list of addresses, modifies the body text and the object and sends the email showing a progressbar.
# FORM BUTTON START
function ButtonStart_Click {
$script:CancelLoop = $false
$ProgressBar1.Visible = $true
$progressbar1.Value = 0
$LabelCounter.visible = $true
$ButtonStart.Enabled = $false
$ButtonStop.Enabled = $true
$TextBoxSubject.ReadOnly = $true
$TextBoxTimeout.ReadOnly = $true
# =============================================================
# LOOP DATA OBJECT
# =============================================================
$Counter = 0
$ErrorCounter = 0
$Timeout = 0
foreach ($Row in $ExcelData) {
[System.Windows.Forms.Application]::DoEvents()
# =============================================================
# Customize mail subject
# =============================================================
$Id = Get-Random -Minimum 99999999
$Subject = ReplaceChars $TextBoxSubject.Text $Id
$MailSettings.Subject = $Subject
# Check timeout input
if ($TextBoxTimeout.Text -match '^[0-9]+$'){
$Timeout = [int]$TextBoxTimeout.Text
}
# =============================================================
# Customize body
# =============================================================
$BodyHTML = ReplaceChars $Body $Row
# =============================================================
# PROGRESSBAR
# =============================================================
if ($script:CancelLoop -eq $true){
$progressbar1.Value = 0
break
}
# Calculate The Percentage Completed
$Counter++
[Int]$Percentage = ($Counter/$ExcelData.Count)*100
$ProgressBar1.Value = $Percentage
$LabelCounter.Text = "Invio $Counter di " +$ExcelData.Count
# =============================================================
# SEND EMAIL
# =============================================================
if ($Row.Email) {
try {
Send-MailMessage -to $Row.Email -Body $BodyHTML -BodyAsHtml @MailSettings -ErrorAction STOP
if ($Timeout -gt 0) {
Sleep $Timeout
}
}
catch {
Log-Write $ErrorLogFile $_.Exception.Message
$ErrorCounter++;
}
}
}
if ($Counter -eq $ExcelData.Count) {
[System.Windows.Forms.MessageBox]::Show("OK!","Info","OK","Information")
$ButtonStop.Enabled = $false
# Show Errors Popup
if ($ErrorCounter -gt 0) {
[System.Windows.Forms.MessageBox]::Show("Error !","Error","OK","Error")
}
$Form.close()
}
# FORM BUTTON STOP
function ButtonStop_Click {
$script:CancelLoop = $true
$ProgressBar1.Visible = $false
$ProgressBar1.Value = 0
$LabelCounter.Visible = $false
$TextBoxSubject.ReadOnly = $false
$TextBoxSubject.Text = ""
$TextBoxTimeout.ReadOnly = $false
}
My problem is that during the execution of the script it seems that the GUI freezes.
Even the stop button sometimes despite being enabled does not seem to interact.
I would like to try launching the loop for generating and sending mail in the background (solving the freeze?), keeping the progressbar and command button in main windows but unfortunately I could not do it (I lost control of the background job).
Can you give me a hand to do this?
Thank you