Click here to Skip to main content
14,696,277 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

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

'calling function
                               If CheckIfRunning() = True Then
                                   ButtonIncrement_Click(Nothing, Nothing)
                               End If

What I have tried:

while CheckIfRunning()=true
Updated 4-Dec-19 7: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.
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
If CheckIfRunning() = True Then
If CheckIfRunning() Then
And for the contrary, instead of
If CheckIfRunning() = False Then
If Not CheckIfRunning() Then
The reason for that is that comparing a boolean to a boolean is redundant and thus unnecessary.

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