Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing another application 1

Status
Not open for further replies.

realm174

Programmer
Jan 3, 2002
154
CA
Good day folks!

I need to write an application that does the following:

- Close an already running program
- Move/rename a file
- Restart the application that was originally running.

Step 2 and 3 should be fairly easy, but is there any docs anywhere explaining how I can terminate a running application?



Cheers,

Realm174
 
This is the fun thing so to speak the best way is to tell program to close itself. Barring that you have to have windows kill the active program which is never a good thing, but sometimes necessary. Assuming for you it is then the other problem is that if you have multiple instances of that running (say Excel or Word) then generally you only option is going to be close all instances. The caveat of that is there are unique things about even those processes, but they can be harder to find. One of the easier is if you can be sure that the title of the window will be unique then you can check for that. Here is an Example of stopping only the note pad window that says "testnotepad.txt - Notepad".

Code:
        Dim myProcesses() As Process
        Dim instance As Process
        
        myProcesses = Process.GetProcessesByName("notepad")

        If myProcesses.Length > 0 Then
            For Each instance In myProcesses
                'Do something to or with each instance of "Notepad" or set a flag if an instance is found... 
                If instance.MainWindowTitle = "testnotepad.txt - Notepad" Then
                    MsgBox(instance.MainWindowTitle)
                    'instance.CloseMainWindow() 'This should be tried first and it attempts to get the program to shutdown correctly.
                    'instance.Close() 'Not quite as good but attempts a force close of the process.
                    'instance.Kill() 'Bad idea to use, but attempts to stop the process no matter what it is doing.
                End If
            Next
        End If


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oh, this is awesome!! I never thought of the different scenarios, so it is very valuable info, just in case. In my situation, there is only one instance running (Procomm). I have tested your code on a dev instance, and it worked perfectly, so I will add that to the rest of the code and see how it works in the live instance.

Thank you so much! I highly appreciate it!!


Cheers,

Realm174
 
NP. I glad it fit your needs. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top