Search code examples
wpfvb.netprocessvirtual

VB.net GetProcess Options


I currently have the following code that runs an external Virtual Application(created with Cameyo), when the Run Button is clicked. I also have a Timer that checks ever second to see if the Process(virtual exe program) is still open. In theory the GetProcessByName should find the program listed in the Task Manager right? However it doesn't! I even tried using GetProcessByName to Kill the process (one another button clicked), but the processs is not killed.

Could it be because I virtualized the program that I want GetProcessByName to recognize? Therefore the name of the Task in the Task Manager is not correct?. Example

The program started: SmartDefrag.virtual.exe

It runs The Task Manager shows it as SmartDefrag.exe Use GetProcessByName("SmartDefrag.exe") to Disable Run Button if process SmartDefrag.exe running.

Does not Disable Run Button.

Could I use TITLE OF PROCESS? Or will the PID be the same everytime the process opens? Any other options?

Code:

Private Sub SMDFRunAppMainButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SMDFRunAppMainButton.Click
' LoadingSMDFMainButton.Visibility = Windows.Visibility.Visible
Dim downloadlocation As String = (currentpath & "\1stAidApps\SMDF\SmartDefrag.virtual.exe")
My.Settings.FileLoad = downloadlocation
    Try 'Errors on Cancel

        dp1Timer = New DispatcherTimer
        dp1Timer.Interval = TimeSpan.FromMilliseconds(1000)
        AddHandler dp1Timer.Tick, AddressOf TickMe1
        dp1Timer.Start()

        fileload = My.Settings.FileLoad

        Process.Start(fileload)
    Catch ex As Exception
        MessageBox.Show("Failed to launch. Please try again.", "Launch Failed")

    End Try
End Sub

Private Sub TickMe1()
    Dim p() As Process

    p = Process.GetProcessesByName("SmartDefrag.exe")

    If p.Count > 0 Then
        LoadingSMDFMainButton.Visibility = Windows.Visibility.Hidden

        SMDFRunAppMainButton.IsEnabled = False
    Else
        SMDFRunAppMainButton.IsEnabled = True
    End If

End Sub

Solution

  • GetProcessByName doesn't take the full path, but rather the "name" of the process. This will likely need to be GetProcessByName("SmartDefrag").

    From the documentation for GetProcessByName:

    The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path.