Results 1 to 13 of 13

Thread: Close process

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Close process

    You could loop through them all looking for a match...
    VB Code:
    1. Dim oProcess As Process
    2. For Each oProcess In System.Diagnostics.Process.GetProcesses()
    3.     If oProcess.ProcessName.ToString = "YourDesiredExe" Then
    4.         MessageBox.Show("YourDesiredExe is running!")
    5.     End If
    6. Next
    Or just populate an array of processes of just the ones of the desired name...
    VB Code:
    1. 'Gets first item
    2. Dim oProcess As Process() = Process.GetProcessesByName("msnmsgr")
    3. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Close process

    VB Code:
    1. If Process.GetProcessesByName(ProcessName).GetLength(0) > 1 Then
    2.             Process.GetProcessesByName(ProcessName)(0).Kill()
    3.          End If

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Close process

    I had to get rid of the tostring???????

    VB Code:
    1. Dim oProcess As Process
    2. For Each oProcess In System.Diagnostics.Process.GetProcesses()
    3.     If oProcess.ProcessName= "YourDesiredExe" Then
    4.         MessageBox.Show("YourDesiredExe is running!")
    5.     End If
    6. Next

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Close process

    2005 express

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Close process

    Mine shows no terminating null character or anything. Matches with the use of .ToString
    Attached Images 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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:
    1. dirSrv.StartInfo.FileName = "myProc.exe"
    2.             dirSrv.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized
    3.             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?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Close process

    I did this and it created a process and killed it too.
    VB Code:
    1. System.Diagnostics.Process.Start("Notepad.exe")
    2. Dim oProcess2 As Process
    3. For Each oProcess2 In System.Diagnostics.Process.GetProcesses()
    4.     If oProcess2.ProcessName.ToString = "notepad" Then
    5.         oProcess2.Kill()
    6.         MessageBox.Show("Process started from code is now killed")
    7.         Exit For
    8.     End If
    9. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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
  •  



Click Here to Expand Forum to Full Width