winforms - Why is my WindowsForm not responding while executing loop -


this may question difficult answer. wrote script checks responding property of process. visualize script running, created windows form can see process watched. script runs perfectly, can't winform. can't minimize or close it, mouse cursor switches hourglass symbol move cursor windowsform. ideas why?

the winform not responding when comment out while loop

here's code:

if ($showwindowsform){     $window = new-object system.windows.forms.form     $window.text = "process watcher"     $window.size = new-object system.drawing.size(350,100)      $window.location = new-object system.drawing.size(100,100)      $icon = [system.drawing.icon]::extractassociatedicon($pshome + "\powershell.exe")     $window.icon = $icon     $text = new-object system.windows.forms.label     $text.text = "folgender prozess wird überwacht:`n$target.exe"     $text.location = new-object system.drawing.size(10,10)      $text.autosize = $true     $window.controls.add($text)     $window.show() } while (1) {     sleep -milliseconds 100     if(!((get-process $target).responding -eq $true)) {     #do stuff } 

i got answer now, if runs same problem have.

first of all, if create gui , processing, use same thread. example:

# windows form     $window = new-object system.windows.forms.form $window.text = "process watcher" $window.size = new-object system.drawing.size(350,100)  $window.location = new-object system.drawing.size(100,100)  $window.showdialog()  # processing while (1) { # stuff } 

powershell show $window because of .showdialog() method, windows form ($window) won't responsive. that's because you're running loop in same thread show windows-form dialog.

so need create background task loop, has thread itself. that's powershell's start-job cmdlet for.

let's you're monitoring process, , want visualize in windows-form. code this:

$target = "firefox"  # job start-job -argumentlist $target {     param($target)     while ((get-process $target).responding) {sleep -milliseconds 100}     if (!(get-process $target).responding) {<# stuff #>} }  # windows form     $window = new-object system.windows.forms.form $window.text = "process watcher" $window.size = new-object system.drawing.size(350,100)  $window.location = new-object system.drawing.size(100,100)  $window.showdialog() 

with code, windows-form responsible, , loop executing in background. want show code example is, have pass variable declared outside job scriptblock job -argumentlist parameter , param() statement. otherwise won't work.

i hope answer because google doesn't give answer (or didn't find one)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -