Results 1 to 11 of 11

Thread: Ending programs in task manager

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Ending programs in task manager

    Hey. I have a program that updates itself. It downloads an update program then I need the update program to check if any isntances of the program are still running and then end them. Is there a way that I can like have the program access the task manager and end all processes that are named "my program" or whatever?

    Thanks
    John

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    Of course there is Its pretty simple too...
    VB Code:
    1. Dim px() As Process = System.Diagnostics.Process.GetProcesses 'gets processes running
    2.         For Each Proc As Process In px 'loops through all processes, finding the name we want
    3.             If Proc.ProcessName = "notepad" Then 'notepad used as a test
    4.                 Proc.CloseMainWindow() 'closes application
    5.             End If
    6.         Next
    The .CloseMainWindow method is preferrable over Proc.Kill, but both could be used. .CloseMainWindow enables it to run its usual close procedures, and any cleanup code it has to run (just like clicking the close button on the form), wheras .Kill just kills the process, wheras any cleanup code would not be ran...

    ***Note - this is assuming the process has a window associated with it, if you try to call .CloseMainWindow on, say, a service, I believe you will get an error... there are other methods to stop services if this is the case...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Ending programs in task manager

    hmm. It's not quite perfect. Like it sometimes closes, and sometimes doesn't. I tried kill() and it still doesn't always close. Any ideas?

    CORRECT: .kill() does work. But I think i would want the .closemainwindow() to work, because that way it closes properly. Any ideas on how to get that to work?
    Last edited by JohnRChick; Jan 14th, 2006 at 03:55 PM.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    It should... I have used code like this several times with no problem. Are you sure you are using the correct processname? Does your application bring any dialogs up while closing? Not quite sure what to tell you. It has always worked with no problem for me.

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    You can try using a WaitForExit method... see if that helps at all... not sure if it will or not..
    VB Code:
    1. Dim px() As Process = System.Diagnostics.Process.GetProcesses
    2.         For Each Proc As Process In px
    3.             If Proc.ProcessName = "notepad" Then
    4.                 Proc.CloseMainWindow()
    5.                 Proc.WaitForExit() '<--- add this line here to wait until the process has fully exited
    6.                 MsgBox("closed")
    7.             End If
    8.         Next

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Ending programs in task manager

    hmm this is very weird! like i tried that and it still doesn't work. If i keep the process name the same, and just use the .kill() instead, it works fine! But I dont really want to use the .kill because in the program I have, I have it set to do some things on exit so it would be missing out of those. I also tried .close() and still no luck

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    Well what is your application doing? Is it running any threads? Is it doing some processing when you are trying to close it? Is the main window (startup form) visible?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Ending programs in task manager

    well the application im trying to close is just sitting there. It doesn't have any threads. OMG I JUST FIXED IT! but i haev another problem now! I have the program set so that it doesn't show itself in the taskbar at the bottom. So I guess thats why it wouldn't close. When I have it set to show in the taskbar it works! I really dont want it in the taskbar though, is there any way to get the closemainwindow to work without the taskbar?

  9. #9
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    Well does this application have a window OPEN when it is running? or is it hidden? Or not present?? If there is no window, then closemainwindow will not work (for obvious reasons)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    Re: Ending programs in task manager

    yeah, it does have a window open...

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ending programs in task manager

    Well all closemainwindow does is request the program to close (like closing the application window). It is the code in the application that is not allowing it to close for some reason, and without seeing the code, I or anyone else wont be able to determine why...

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