Click here to Skip to main content
15,888,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check an existing file, if the process still waiting for the file, it will display a GUI window. After the file is exist, the window will close automatically.

I tried this code, the window can not close, even the file already exist.

What I have tried:

Checking the file:

$SN = "708TSTA"
$MAC = "2E5961370"

function Find {
    $n = 0
    while (-not (Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"})) {
        Start-Sleep -s 1
        D:\Auto\GUI.ps1
        $n++
        (Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"})
        Write-Host "Attempt no $n"
    }

    Write-Host ">>Flag found after $n attempts"
    return $true
}

if (Find) {
    Write-Host "Found"
}


GUI.ps1:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                 = New-Object System.Windows.Forms.Form
$Form.ClientSize      = '578,400'
$Form.Text            = "Form"
$Form.BackColor       = "#c1daf7"
$Form.WindowState     = 'Maximized'
$Form.FormBorderStyle = "FixedDialog"

$Label1               = New-Object System.Windows.Forms.Label
$Label1.Text          = "UNDER PROCESS"
$Label1.AutoSize      = $true
$Label1.Width         = 25
$Label1.Height        = 10
$Label1.Location      = New-Object System.Drawing.Point(600,300)
$Label1.Font          = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor     = "#d0021b"

$Label2               = New-Object System.Windows.Forms.Label
$Label2.Text          = "WAITING"
$Label2.AutoSize      = $true
$Label2.Width         = 25
$Label2.Height        = 10
$Label2.Location      = New-Object System.Drawing.Point(770,500)
$Label2.Font          = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor     = "#fb0505"

$Check = Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"}
if($Check) {
    Write-Host "File Exist"
    $Form.Close()
}

$Form.Controls.AddRange(@($Label1,$Label2))
[void]$Form.ShowDialog()
Posted
Updated 19-Jul-19 4:06am

1 solution

ShowDialog waits until the window is closed. Your script will not continue running until the user closes the window manually.

You need to use Show instead. You'll need to keep a reference to the form in a variable so that you can close it when the file is found. That will be easier to do if you keep your script in a single file.

For example:
PowerShell
function CreateForm {
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                 = New-Object System.Windows.Forms.Form
    $Form.ClientSize      = '578,400'
    $Form.Text            = "Form"
    $Form.BackColor       = "#c1daf7"
    $Form.WindowState     = 'Maximized'
    $Form.FormBorderStyle = "FixedDialog"

    $Label1               = New-Object System.Windows.Forms.Label
    $Label1.Text          = "UNDER PROCESS"
    $Label1.AutoSize      = $true
    $Label1.Width         = 25
    $Label1.Height        = 10
    $Label1.Location      = New-Object System.Drawing.Point(600,300)
    $Label1.Font          = 'Microsoft Sans Serif,30,style=Bold,Underline'
    $Label1.ForeColor     = "#d0021b"

    $Label2               = New-Object System.Windows.Forms.Label
    $Label2.Text          = "WAITING"
    $Label2.AutoSize      = $true
    $Label2.Width         = 25
    $Label2.Height        = 10
    $Label2.Location      = New-Object System.Drawing.Point(770,500)
    $Label2.Font          = 'Microsoft Sans Serif,20,style=Bold'
    $Label2.ForeColor     = "#fb0505"

    $Form.Controls.AddRange(@($Label1,$Label2))
    return $Form
}

function Find($folder, $pattern) {
    $n = 0
    $form = $null
    while (-not (Get-ChildItem -Name $folder | Where-Object {$_ -like $pattern})) {
        if ($form -eq $null) {
            $form = CreateForm
            $form.Show()
        }
        
        Start-Sleep -s 1
        $n++
        
        Write-Host "Attempt no $n"
    }
    
    if ($form -ne $null) {
        $form.Close()
        $form.Dispose()
    }
    
    Write-Host ">>Flag found after $n attempts"
    return $true
}

$SN = "708TSTA"
$MAC = "2E5961370"
$folder = "D:\SERVER\"
$pattern = "*$SN-$MAC*"

if (Find $folder $pattern) {
    Write-Host "Found"
}

Form.ShowDialog Method (System.Windows.Forms) | Microsoft Docs[^]
Form.Show(IWin32Window) Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900