|
-
Jan 14th, 2006, 02:40 PM
#1
Thread Starter
Addicted Member
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
-
Jan 14th, 2006, 03:11 PM
#2
Re: Ending programs in task manager
Of course there is Its pretty simple too...
VB Code:
Dim px() As Process = System.Diagnostics.Process.GetProcesses 'gets processes running
For Each Proc As Process In px 'loops through all processes, finding the name we want
If Proc.ProcessName = "notepad" Then 'notepad used as a test
Proc.CloseMainWindow() 'closes application
End If
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...
-
Jan 14th, 2006, 03:46 PM
#3
Thread Starter
Addicted Member
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.
-
Jan 14th, 2006, 03:58 PM
#4
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.
-
Jan 14th, 2006, 04:01 PM
#5
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:
Dim px() As Process = System.Diagnostics.Process.GetProcesses
For Each Proc As Process In px
If Proc.ProcessName = "notepad" Then
Proc.CloseMainWindow()
Proc.WaitForExit() '<--- add this line here to wait until the process has fully exited
MsgBox("closed")
End If
Next
-
Jan 14th, 2006, 04:22 PM
#6
Thread Starter
Addicted Member
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
-
Jan 14th, 2006, 04:25 PM
#7
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?
-
Jan 14th, 2006, 04:32 PM
#8
Thread Starter
Addicted Member
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?
-
Jan 14th, 2006, 04:40 PM
#9
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)
-
Jan 14th, 2006, 05:22 PM
#10
Thread Starter
Addicted Member
Re: Ending programs in task manager
yeah, it does have a window open...
-
Jan 14th, 2006, 05:45 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|