Code doesn't continue after end process function

ganjeii

Member
Joined
Oct 22, 2014
Messages
7
Location
Boston, MA
Programming Experience
Beginner
I'm writing a small application to clear temp files on a Windows 7/8 machine and to check for, and close Adobe Reader if it is running before continuing. For some reason after Adobe Reader is closed, the code seems to cease and the temp file removal process never starts, this is all done calling subs and functions under a single click event. Please let me know if more code is required aside from what I have posted below:


In this case the process is "AcroRd32"

VB.NET:
'Checks for and prompts to close process
    Public Sub checkProcess(ByVal procN As String)
        Dim pr() As Process


        pr = Process.GetProcessesByName(procN)
        If pr.Count > 0 Then
            Dim cont As Boolean = True
            Dim pick As Integer = MessageBox.Show _
                                (procN & " is running. Press OK to close or Cancel to exit.", _
                                 procN & " running", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)


            If pick = DialogResult.OK Then
                endProcess(procN)
            ElseIf pick = DialogResult.Cancel Then


            End If
        Else
        End If
    End Sub
And I call checkProcess within the button btnKillFolders. Then check to see if Reader is running, if the process count is less than 1 then continue to execute the code within the IF statement, but it just stops. I have gotten it to proceed if I add a MessageBox after the process is killed in the above function, and click OK.


I had also tried a simple wait command after calling checkProcess("AcroRd32") just in-case the process wasn't closed completely when the IF statement was reached. But with no luck.

VB.NET:
'Executes subs to clean temps
    Private Sub btnKillFolders_Click(sender As Object, e As EventArgs) Handles btnKillData.Click
        checkProcess("AcroRd32")
        Dim proc() As Process
        proc = Process.GetProcessesByName("AcroRd32")


        If proc.Count < 1 Then
            endProcess("armsvc")
            endProcess("AdobeARM")
            endProcess("iexplore")
            FixWeb()
            fixAdobe()
        Else
        End If
    End Sub
If I click the button again, then the IF statement executes, but I have to click the button a second time after Adobe Reader is closed because for some reason the IF statement is never executed. I don't want the user to have to do this. I may be completely off base, is there a better way to do this? Or am I just missing something simple? Any feedback is greatly appreciated, and please let me know if any further code is needed. Thanks!
 
Presumably the process with that name still exists when you call GetProcessesByName. You might look at the Process.WaitForExit method for a way to wait until a process has exited before continuing.
 
Hey there! I Just wanted to let you know that I got it working. I was unable to find a way to use the Process.WaitForExit method to work in the way that I want to incorporate subs to check through the processes and close whichever one I choose with ByVal. But I was able to get it working with a Do loop as shown below.

VB.NET:
[COLOR=#008000]'Checks for and prompts to close process[/COLOR]
    Public Sub [B]checkProcess[/B]([B][COLOR=#ff0000]ByVal[/COLOR][/B] [B]procN[/B] As String)
        Dim pr() As Process


        pr = Process.GetProcessesByName
   
        If pr.Count > 0 Then
            Dim pick As Integer = MessageBox.Show _
                                (procN & " is running. Press OK to close.", _
                                 procN & " running", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)


            If pick = DialogResult.OK Then
                [B][COLOR=#0000ff]Do[/COLOR][/B]
                    [B][COLOR=#0000ff]On Error Resume Next[/COLOR][/B]
                    endProcess([B]procN[/B])
                    pr = Process.GetProcessesByName([B]procN[/B])
                [B][COLOR=#0000ff]Loop Until[/COLOR][/B] pr.Count < 1
            ElseIf pick = DialogResult.Cancel Then


            End If
        Else
        End If
    End Sub

It attempts to end the process value (procN) and continues if it errors out as p(i) will throw an access denied error if the process is already closed in my function below to end a specified process:

VB.NET:
[COLOR=#008000]'Kill process Function[/COLOR]
    Public Function [B]endProcess[/B](ByVal [B]procName [/B]As String)
        Dim [B]p() [/B]As Process = Process.GetProcesses


        For i As Integer = 0 To p.GetUpperBound(0)
            If [B]p(i)[/B].ProcessName = [B]procName [/B]Then
                [B]p(i)[/B].Kill()
            End If
        Next
        Return Nothing
    End Function

My checkProcess() sub then checks for the process procN by name, if the count is less than 1 then the code will continue to run to the IF statement that I need it to :tongue:.

VB.NET:
[COLOR=#008000]'Executes subs to clean temps[/COLOR]
    Private Sub [B]btnKillFolders_Click[/B](sender As Object, e As EventArgs) Handles btnKillData.Click
        Dim ieReadValue As String
        Dim proc() As Process


            checkProcess("AcroRd32")
            ieReadValue = ieRegKey.GetValue("SyncMode5", 3)
        proc = Process.GetProcessesByName("AcroRd32")


        [B][COLOR=#0000ff]If [/COLOR][/B]proc.Count < 1 [COLOR=#0000ff][B]Then[/B][/COLOR]
            endProcess("armsvc")
            endProcess("AdobeARM")
            endProcess("iexplore")
            FixWeb()
            fixAdobe()
            [COLOR=#0000ff][B]If [/B][/COLOR]ieReadValue = 3 [COLOR=#0000ff][B]Then[/B][/COLOR]
[COLOR=#0000ff][B]            Else[/B][/COLOR]
                webPageCheck()
[COLOR=#0000ff][B]            End If[/B][/COLOR]
[COLOR=#0000ff][B]        End If[/B][/COLOR]
    End Sub

Thanks for your help jm as you set me in the right direction to get this going! Please let me know if anything isn't clear and I will do my best to re-word anything, in-case it isn't easily understandable for anyone who might stumble on this thread. As I haven't been able to find an instance like this out there yet.
 
Use this method rather than Kill: Process.CloseMainWindow Method (System.Diagnostics) (more explanations in Remarks)

Use Try-Catch rather than On Error.

Code that may take a little while to complete should not block UI thread (event handler), but be performed on a secondary thread.
 
Thanks so much for the suggestion John, I implemented the Try-Catch statement AND the Process.CloseMainWindow() method. They work great!! Thanks so much for your help guys, I have been struggling with this for a week!


VB.NET:
[COLOR=#008000]'Checks for and prompts to close process[/COLOR]
    Public Sub checkProcess(ByVal procN As String)
        Dim pr() As Process


        pr = Process.GetProcessesByName(procN)
        If pr.Count > 0 Then
            Dim pick As Integer = MessageBox.Show _
                                (procN & " is running. Press OK to close.", _
                                 procN & " running", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)


            If pick = DialogResult.OK Then
                Do
                    [B][COLOR=#0000cd]Try[/COLOR][/B]
                        endProcess(procN)
                        pr = Process.GetProcessesByName(procN)
                    [B][COLOR=#0000cd]Catch[/COLOR] [/B]ex [COLOR=#0000cd][B]As Exception[/B][/COLOR]
                        MsgBox("Error: " & ex.Message)
                    [COLOR=#0000cd][B]End Try[/B][/COLOR]
                Loop Until pr.Count < 1
            ElseIf pick = DialogResult.Cancel Then


            End If
        Else
        End If
    End Sub
:beer:
We can consider this solved, I just can't find the solved button haha.
 
Back
Top