Process.GetProcessesByName
returns a snapshot of the running processes at the time you call it. You're only calling it once, outside of your loop. So if the process isn't running when your loop starts, the loop will never end.
Try something like this instead:
Do Until Process.GetProcessesByName("photoshop").Length > 0
System.Threading.Thread.Sleep(5000)
Loop
NB: If you're doing this on the UI thread, your application will be frozen until the process starts. You should consider using
Async
and
Await
instead:
Asynchronous Programming with Async and Await (Visual Basic) | Microsoft Docs[
^]
For example:
Public Shared Async Function WaitForProcessAsync(ByVal name As String, ByVal cancellationToken As CancellationToken) As Task(Of Process)
Do Until cancellationToken.IsCancellationRequested
Dim p As Process() = Process.GetProcessesByName(name)
If p.Length > 0 Then Return p(0)
Await Task.Delay(5000, cancellationToken)
Loop
Return Nothing
End Function