Feb 5th, 2007, 03:25 PM
#1
Thread Starter
Hyperactive Member
Close process
Is there any way to determine if a process is running on my PC and shut it down if it is? I know that the process appears in the Task Manager processes window whien it's running so it seems like there should be a way to index through all of the running processes until I find the one that I want.
Feb 5th, 2007, 03:33 PM
#2
Re: Close process
You could loop through them all looking for a match...
VB Code:
Dim oProcess As Process
For Each oProcess In System.Diagnostics.Process.GetProcesses()
If oProcess.ProcessName.ToString = "YourDesiredExe" Then
MessageBox.Show("YourDesiredExe is running!")
End If
Next
Or just populate an array of processes of just the ones of the desired name...
VB Code:
'Gets first item
Dim oProcess As Process() = Process.GetProcessesByName("msnmsgr")
MessageBox.Show(oProcess.GetUpperBound(0).ToString)
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Feb 5th, 2007, 03:35 PM
#3
Fanatic Member
Re: Close process
VB Code:
If Process.GetProcessesByName(ProcessName).GetLength(0) > 1 Then
Process.GetProcessesByName(ProcessName)(0).Kill()
End If
Feb 5th, 2007, 03:42 PM
#4
Thread Starter
Hyperactive Member
Re: Close process
I'm missing something. While in debug I can step through this code and see the process thatI want to close appear in the oProcess.Processname field but the if statement is not executed. I've tried matching case but it made no difference. Do I need to include something else?
Feb 5th, 2007, 03:45 PM
#5
Thread Starter
Hyperactive Member
Re: Close process
I had to get rid of the tostring???????
VB Code:
Dim oProcess As Process
For Each oProcess In System.Diagnostics.Process.GetProcesses()
If oProcess.ProcessName= "YourDesiredExe" Then
MessageBox.Show("YourDesiredExe is running!")
End If
Next
Feb 5th, 2007, 03:47 PM
#6
Re: Close process
Weird, works for me under 2003 sp-1. What version are you running? Maybe there is a null character terminator within the array element.
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Feb 5th, 2007, 03:49 PM
#7
Thread Starter
Hyperactive Member
Feb 5th, 2007, 04:00 PM
#8
Re: Close process
Mine shows no terminating null character or anything. Matches with the use of .ToString
Attached Images
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Feb 5th, 2007, 04:09 PM
#9
Thread Starter
Hyperactive Member
Re: Close process
I think that you're right. Here's what is happening. I'm starting a process within my code but this process will not run if an instance already exists. The plan was to check to see if it exists and if it does, close it before I start a new instance.
Now for the strange part. When I tried your code originally, the proces was started by by code
VB Code:
dirSrv.StartInfo.FileName = "myProc.exe"
dirSrv.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized
dirSrv.Start()
I treid to uose your code to shut down this process and it did not work. I shut it down manually and remove the tostring from my code. This time I manually started the process and ran my code. It worked. I assumed that the problem was the tostring until the next time that I ran the process no longer shut down. It appears that I cannot close this process if I start it from my code butI can if I manually start the process.
Does this make ANY sense?
Feb 5th, 2007, 04:16 PM
#10
Re: Close process
I did this and it created a process and killed it too.
VB Code:
System.Diagnostics.Process.Start("Notepad.exe")
Dim oProcess2 As Process
For Each oProcess2 In System.Diagnostics.Process.GetProcesses()
If oProcess2.ProcessName.ToString = "notepad" Then
oProcess2.Kill()
MessageBox.Show("Process started from code is now killed")
Exit For
End If
Next
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Feb 5th, 2007, 04:16 PM
#11
Thread Starter
Hyperactive Member
Case Sensitivity
I guess that it was case sensitivity. If I created the process manually the name was in all caps but when I created it in code the name was all lower case.
Feb 5th, 2007, 04:18 PM
#12
Re: Close process
Yes, it is case sensitive.
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Feb 5th, 2007, 04:21 PM
#13
Thread Starter
Hyperactive Member
Re: Close process
Thanks for the help. I've been out of the Vb world for over a year. It's amazing how fast the knowledge goes away. But I'm pretty good at c++ and Fortran.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width