Click here to Skip to main content
16,007,814 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hii i want to do loop that check if the process is exist

My code is:

Dim RunProc As Process() = Process.GetProcessesByName("photoshop")

Do Until RunProc.Length > 0
            Label1.Text = "game running"
            Delay(5)

            Exit Do
        Loop

I want to do loop that runs sleep(5000) until RunProc = photoshop process
Tnk

What I have tried:

I tried to do some loop with "until loop"
Posted
Updated 1-Aug-19 8:27am
v2
Comments
Dave Kreskowiak 1-Aug-19 0:09am    
And your problem with this little task is what?
OriginalGriff 1-Aug-19 1:10am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

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:
VB.NET
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:
VB.NET
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
 
Share this answer
 
v2
Comments
MasterGamerFX 2-Aug-19 8:54am    
ok ,i try but i think about another option

but i thought how to do if that process not started in 2 seconds then do something like msgbox ..
Richard Deeming 2-Aug-19 9:07am    
Use the Async version I posted:
Dim cts As New CancellationTokenSource(TimeSpan.FromSeconds(2))
Dim p As Process = Await WaitForProcessAsync("photoshop", cts.Token)
If p Is Nothing Then MessageBox.Show("...")

You'll obviously want to decrease the delay within the method from the current five seconds - eg:
Await Task.Delay(500, cancellationToken)
MasterGamerFX 2-Aug-19 9:24am    
ok but where i place the code ?
Richard Deeming 2-Aug-19 9:33am    
You put the function somewhere where it can be called by the code that needs to wait for the process.

You put the code to call that function where you want to wait for the process.
MasterGamerFX 2-Aug-19 12:16pm    
i try but does have errors
need help explain me better please

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