Results 1 to 3 of 3

Thread: How to terminate the previous instance of my application

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005

    How to terminate the previous instance of my application

    Hi all,

    I have the following code to detect if my application is already running; if so, how can i terminate the previous instance of my application.
    VB Code:
    1. If App.PrevInstance Then
    2.   'App is already running
    3.   'Here I want to unload the previous instance
    4. End If
    Thanks in advance.

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Sharjah, United Arab Emirates

    Re: How to terminate the previous instance of my application

    you have to use Kill Process for this, to find out the process you can use either FindWindow or something like that...just search for Kill process, you will hit a lot of examples and codes...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.

  3. #3
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Mars,Milky Way... Chit Chat Posts: 5,733

    Re: How to terminate the previous instance of my application

    ganeshmoorthy, The kill process actually deletes the file off the hard disk.

    Try this, Not sure how can you use it:

    VB Code:
    1. Private Sub Command1_Click()
    2.     ShutWindow "Untitled - Notepad"
    3. End Sub

    In A Module:

    VB Code:
    1. Option Explicit
    2.  Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
    3.  Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    4.         ByVal hwnd As Long, _
    5.             ByVal nIndex As Long) As Long
    6.  Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    7.         ByVal hwnd As Long, _
    8.             ByVal wMsg As Long, _
    9.                 ByVal wParam As Long, _
    10.                     ByVal lParam As Long) As Long
    11.  Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    12.         ByVal lpClassName As Any, _
    13.             ByVal lpWindowName As String) As Long
    14.     Public Const GWL_STYLE = -16
    15.         Public Const WS_DISABLED = &H8000000
    16.                  Public Const WM_CLOSE = &H10
    18. Public Function ShutWindow(Window_Title As String)
    19.     Dim X As Long
    20.         Dim WindowHwnd As Long
    21.             WindowHwnd = FindWindow(0&, Window_Title)
    22.     If WindowHwnd = 0 Then Exit Function
    23.         If IsWindow(WindowHwnd) = False Then
    24.                 Else
    25.             If Not (GetWindowLong(WindowHwnd, GWL_STYLE) And WS_DISABLED) Then
    26.                 X = PostMessage(WindowHwnd, WM_CLOSE, 0, 0&)
    27.             End If
    28.         End If
    29.  End Function
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:

    5)Support Classic VB - A PETITION TO MICROSOFT

    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

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