0

What I have:

Process.Start("powershell.exe", "-executionpolicy remotesigned -file C:\temp\test.ps1")


        Do Until (ready = True)

            If Process.GetProcessesByName("powershell").Length <> 0 Then
                ready = False
            Else
                ready = True
            End If
        Loop

... more code ...

Currently my program freezes while powershell is active. If the powershell script takes too long time, more then 60 seconds, my program also crashes. How finally make it smooth, like using a timer so that it does not freeze anymore.

EDIT:

Finally I'd like to check every 5 seconds if powershell is active.

EDIT 2:

I can just do

Dim test As Process = Process.Start("powershell.exe", "-executionpolicy
remotesigned -file C:\temp\test.ps1")

    test.WaitForExit()

And it does not crashes anymore, but it still freezes :/

hannir
  • 95
  • 1
  • 10
  • Possible duplicate of [Wait till a process ends](http://stackoverflow.com/questions/3147911/wait-till-a-process-ends) – Nico Schertler Jul 01 '16 at 11:31
  • 1
    Note (not related to the problem): `While ready = False` and `Do Until ready = True` do the same thing, you need to use only one of them. – 41686d6564 stands w. Palestine Jul 01 '16 at 11:40
  • Thx @Nico , sry for flag comment. After some trys u are right - it does not crahses after 60 seconds anymore, but it still freezes while processing. Possible to fix the freezes? – hannir Jul 01 '16 at 12:32
  • 1
    Just use the `Exited` event to get a notification when the process exits (see the second answer in the linked question). – Nico Schertler Jul 01 '16 at 12:47

1 Answers1

0

Try using another thread, it will prevent your main thread from freezing. To understand how the threads work check some tutorials about threading like this one.

Dim Thread1 As Threading.Thread

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Creates a new thread
    Thread1 = New Threading.Thread(Sub() MyProcess())
    'Starts the thread
    Thread1.Start()
End Sub

Private Sub MyProcess()
    Process.Start("powershell.exe", "-executionpolicy remotesigned -file C:\temp\test.ps1")

    Do Until (ready = True)

        If Process.GetProcessesByName("powershell").Length <> 0 Then
            ready = False
        Else
            ready = True
        End If
    Loop
End Sub
David -
  • 2,009
  • 1
  • 13
  • 9