locked
Close a program when another program Close??? RRS feed

  • Question

  • Hi Friends, Is it possible to close a program when another program close, For instance....Say I have program A and B.... I want program B to be close when close or exit program A... I need help on this.. Please... Thanks in advance
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 2:06 AM

Answers

  • You can use Management to do this if the application has administrator privilege

    In This example the application closes if  Notepad is closed

    get a reference to System.Management

     

    Imports System.Management
    
    Public Class Form1
    
    
    
     Private WithEvents EventWatcher As New ManagementEventWatcher
    
    
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      Dim WQLquery As New WqlEventQuery("__InstanceDeletionEvent", New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")
    
      EventWatcher.Query = WQLquery
    
     End Sub
    
    
    
     Private Sub EventWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs) Handles EventWatcher.EventArrived
    
      Dim WMI_Event = e.NewEvent
    
      Dim MBO As ManagementBaseObject = CType(WMI_Event.GetPropertyValue("TargetInstance"), ManagementBaseObject)
    
      Dim ClosingProcessName As String = CStr(MBO.GetPropertyValue("Name"))
    
      If ClosingProcessName = "notepad.exe" Then '<====Change Notepad for the name of the application that cause tis one to close
    
       Me.Invoke(New Action(AddressOf CloseMe))
    
      End If
    
     End Sub
    
    
    
     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    
      EventWatcher.Stop()
    
     End Sub
    
    
    
     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    
      EventWatcher.Start()
    
     End Sub
    
    
    
     Private Sub CloseMe()
    
      Me.Close()
    
     End Sub
    
    End Class
    
    

     


    • Edited by Crazypennie Monday, March 21, 2011 3:28 AM
    • Marked as answer by Han_BFY Monday, March 21, 2011 4:02 AM
    Monday, March 21, 2011 3:20 AM

All replies

  • There are several ways in which program A can send a message to program B to indicate that it is closing, and that program B should also close.  But they rely on program A and program B both being programs that you have written so that you can include the appropriate communication code.    Are you writing both program A and program B, or is one or the other of these programs a commercial application that you cannot modify?
    Monday, March 21, 2011 2:25 AM
  • Hi Acamar, Yes... One of the program is commercial .... and one is I created one... Is it possible...???
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 2:37 AM
  • Which is which?

    If you want your program to close when the commercial program is closed, that will depend on some signal being available from that program.  A possibility is a temporary file that the commercial program uses - when it exists, the program is running: when it is deleted, the program is closed.  So your program periodically checks the file, and if it isn't found then the program closes. For a permanent workfile you can use a similar technique, but you try to open it instead of just checking that it exists - if you can open it then the commercial program is closed.   It all depends on what that program does.

    Another option is to examine all the processes running on the PC and see if that program is one of them  - if it isn't found, then your program should close.  Note that this periodic testing creates problems at startup - without additional code you will need to ensure that the commercial program is always started before yours, otherwise your program will shut down at the very first test.

    If you want to close the commercial program when your program closes, that may be a bit simpler.  You can use SendKeys to send a keystroke (such as Alt+F4) when your program closes, but that assumes that the other program supports closing like this and won't pop up a dialog about saving files or confirming the closedown.  You can also find the process and terminate it, although that could have implications if the commercial program is in the middle of some processing, such as writing to a file.

    Like I say - there's lots of options, but you need to determine the exact procedure you want to follow, and the way that commercial program works will be very important.


    Monday, March 21, 2011 3:06 AM
  • In my case I want to close my program if the commercial program Close... I really appreciate If you can guide me to which Chapter... or thread or any source should I refer... to get what i need. Thansk in advance
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 3:16 AM
  • You can use Management to do this if the application has administrator privilege

    In This example the application closes if  Notepad is closed

    get a reference to System.Management

     

    Imports System.Management
    
    Public Class Form1
    
    
    
     Private WithEvents EventWatcher As New ManagementEventWatcher
    
    
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      Dim WQLquery As New WqlEventQuery("__InstanceDeletionEvent", New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")
    
      EventWatcher.Query = WQLquery
    
     End Sub
    
    
    
     Private Sub EventWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs) Handles EventWatcher.EventArrived
    
      Dim WMI_Event = e.NewEvent
    
      Dim MBO As ManagementBaseObject = CType(WMI_Event.GetPropertyValue("TargetInstance"), ManagementBaseObject)
    
      Dim ClosingProcessName As String = CStr(MBO.GetPropertyValue("Name"))
    
      If ClosingProcessName = "notepad.exe" Then '<====Change Notepad for the name of the application that cause tis one to close
    
       Me.Invoke(New Action(AddressOf CloseMe))
    
      End If
    
     End Sub
    
    
    
     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    
      EventWatcher.Stop()
    
     End Sub
    
    
    
     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    
      EventWatcher.Start()
    
     End Sub
    
    
    
     Private Sub CloseMe()
    
      Me.Close()
    
     End Sub
    
    End Class
    
    

     


    • Edited by Crazypennie Monday, March 21, 2011 3:28 AM
    • Marked as answer by Han_BFY Monday, March 21, 2011 4:02 AM
    Monday, March 21, 2011 3:20 AM
  • The other way is to use a timer and every so often get the list of all the process rinning in the computer. If the application stop being in the list, it means that it was closed and that you need to close your application also
    Monday, March 21, 2011 3:27 AM
  • Have you investigated that program to see if there is some file that can indicate what you need to know?

    Information about getting running processes is here - if you can reliably find the process that you are interested in, then periodically checking whether or not it is still running is probably the simplest way to go.

    Monday, March 21, 2011 3:28 AM
  • CrazyPennie,

    Not intending to hijack this thread, but why do you state:"if the application has administrator privilege"?

    I just ran your code and it works great.  I run under a normal user account in XP.

     

    Monday, March 21, 2011 3:46 AM
  • Hi Crazypennie, I get erro on this code On the Action words "Me.Invoke(New Action(AddressOf CloseMe))" Error Description is "Too few type arguments to 'system.Action(OF T)'" can help me on this??
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 3:46 AM
  • It's done.... I change .NET framework 3.0 to 3.5... Thanks a lot to you guys
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 4:02 AM
  • Hi Anyone know to run exe using batch file command I need help... Thanks in advance
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 4:58 AM
  • DONE
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Monday, March 21, 2011 5:09 AM