Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, How can I repeat again and again code if process still running?

I want to check if program still running, if yes, check again and again, if no continue in execute next part of code.

Function CheckIfRunning() As Boolean
        p = Process.GetProcessesByName("Ls32") 
        
        If p.Count > 0 Then
            ' Process is running
            CheckIfRunning = True

        Else
            ' Process is not running
            CheckIfRunning = False
        End If
    End Function


'calling function
CheckIfRunning()
                               If CheckIfRunning() = True Then
                                   CheckIfRunning()
                                   Threading.Thread.Sleep(800)
                               Else
                                   ButtonIncrement_Click(Nothing, Nothing)
                               End If


What I have tried:

while CheckIfRunning()=true
Loop
Posted
Updated 4-Dec-19 6:20am

The problem is that if you loop on your function, your UI thread is blocked until the process is completed - and if it's blocked, it can;t respond to user input and will "lock up" as far as the user is concerned.

I'd suggest that you look at a BackgroundWorker thread to process your loop, and to report "Process complete" back to your UI thread.
BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^] - it includes an example.
 
Share this answer
 
Comments
Member 13711215 5-Dec-19 2:59am    
Thank you
OriginalGriff 5-Dec-19 3:13am    
You're welcome!
Aside from solution 1 which is quite relevant, I would advise you not to compare a boolean to a boolean.
Instead of
VB
If CheckIfRunning() = True Then
write
VB
If CheckIfRunning() Then
And for the contrary, instead of
VB
If CheckIfRunning() = False Then
write
VB
If Not CheckIfRunning() Then
The reason for that is that comparing a boolean to a boolean is redundant and thus unnecessary.
 
Share this answer
 

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