How can I kill a process with GetProcessesByName ?

Ask general questions here.
rastek
Posts: 157
Joined: Wed Aug 06, 2014 12:00 pm

How can I kill a process with GetProcessesByName ?

Post by rastek » Sun Oct 09, 2016 1:31 pm

Hi,
here is my vb code, but when I try to compile Ranorex gives process not foud error, please help me.

Dim proc = Process.GetProcessesByName("RealtimeConnectionsClient.exe")
For i As Integer = 0 To proc.Count - 1
proc(i).CloseMainWindow()
Next i

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3922
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How can I kill a process with GetProcessesByName ?

Post by odklizec » Mon Oct 10, 2016 9:28 am

Hi,

I'm not a VB.Net expert, but the code looks OK to me? Just one thing, use "RealtimeConnectionsClient" instead of "RealtimeConnectionsClient.exe" as the process name and make sure the process name is correct! I believe the processname must be always without the ".exe" extension.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: How can I kill a process with GetProcessesByName ?

Post by Aracknid » Tue Oct 11, 2016 3:53 pm

GetProcessByName returns an array, and you didn't declare proc as an array.

Try
Dim proc() as process = nothing
proc = Process.GetProcessesByName("RealtimeConnectionsClient")
For i As Integer = 0 To proc.Count - 1
    proc(i).CloseMainWindow()
Next I

rastek
Posts: 157
Joined: Wed Aug 06, 2014 12:00 pm

Re: How can I kill a process with GetProcessesByName ?

Post by rastek » Sat Oct 22, 2016 2:37 pm

Thanks Aracknid
I tried your suggestion and Ranorex compiler returned me this error, can you help for this error ?

'Count' is not a member of 'System.Array'. (BC30456)


And here is my full code part for the function, do I miss a library or sth ?

I also tried Host.Local.KillApplication which is comment out in the code.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Drawing
Imports System.Threading
Imports WinForms = System.Windows.Forms

Imports System.Diagnostics
Imports System.ComponentModel

Imports Ranorex
Imports Ranorex.Core
Imports Ranorex.Core.Testing

Namespace Ras

Public Partial Class Login_Start

''' <summary>
''' This method gets called right after the recording has been started.
''' It can be used to execute recording specific initialization code.
''' </summary>
Private Sub Init()
' Your recording specific initialization code goes here.
End Sub

Sub KillProcess()
''Host.Local.KillApplication("RealtimeConnectionsClient")
Dim proc() as process = nothing
proc = Process.GetProcessesByName("RealtimeConnectionsSV2.exe")
For i As Integer = 0 To proc.Count - 1
proc(i).CloseMainWindow()
Next i
End Sub

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: How can I kill a process with GetProcessesByName ?

Post by Aracknid » Sat Oct 22, 2016 6:25 pm

First, you still have ". Exe" in your string of the process name, which is wrong and leads to no matches found for proc, so proc = nothing, and that is the run time error. Remove ". Exe" and add a line of code to not do the if, if proc is nothing.

Aracknid

rastek
Posts: 157
Joined: Wed Aug 06, 2014 12:00 pm

Re: How can I kill a process with GetProcessesByName ?

Post by rastek » Sat Oct 22, 2016 9:37 pm

Aracknid, my real problem was not 'not finding' because of exe but it was not even compiling..

Ok I just replaced .count with length and it compiled thanks.

My second question is, I have more than one exe to kill, can I add more exe to proc, since its an array ?

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: How can I kill a process with GetProcessesByName ?

Post by Aracknid » Mon Oct 24, 2016 2:34 pm

I think at this point in time, it would be a good idea for you to read the online help available for handling or getting processes. You'll find a lot of useful functions and examples that should help you get through this.

https://msdn.microsoft.com/en-us/librar ... .110).aspx

Aracknid