locked
PowerShell Form Running Detached RRS feed

  • Question

  • Hello,

    I'm developing a Windows based form from PowerShell, and i'm struggling with interaction with the main HostUI form, when on of the actions on the form has been selected.

    In essence, when you click on a button on my form, it fires up a separate PS and calls a script to do some stuff, however whilst that second window remains open, i still want the buttons on the original form to be active. Currently i can only use the buttons again once i've closed the new PS window.

    My question, how can i invoke or call this in a somewhat disconnected state, so both windows are still interactive?

    I've tried adding &, and calling the -InDisconnectedSession but i'm not getting any joy.

    invoke-expression 'cmd /c start powershell -noexit -inputformat text -outputformat text -Command { c:\script\ConnectUP.ps1 }'

    Any help would be very much appreciated.

    Thanks

    Wednesday, June 5, 2019 12:41 PM

All replies

  • You cannot block in an event without freezing the form.

    You can use a job or a runspace to run the external code.  With either you will have to poll the external process on a timer to test for completion.

    Here is an article that explains how this has to work:

    https://info.sapien.com/index.php/guis/gui-advanced-tips/powershell-studio-creating-responsive-forms


    \_(ツ)_/

    Wednesday, June 5, 2019 1:02 PM
  • You can also try this:

    $p = Start-Process powershell -ArgumentList '-NoExit -FIle c:\script\ConnectUP.ps1' -PassThru
    While(-not $p.HasExited )  {
        [windows.forms.application]::DoEvents()
        Sleep -m 200
    }


    \_(ツ)_/

    Wednesday, June 5, 2019 1:11 PM