Is it possible to link a process (i.e. osk.exe) opened by a program so it dies when calling program dies?
So, I start my application, it starts OSK.EXE, and if the program crashes, the on-screen keyboard is still there. Possible to link the two so that the process dies when the application dies?

Recommended Answers

All 10 Replies

See if this helps.
1 Timer

Public Class Form1
    Private myRunningProcesses() As Process
    Private myAPPthatCrashes As String = "notepad", myKeyboardApp As String = "calc", bAppFound As Boolean

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With Timer1
            .Interval = 1000 '// 1 sec.
            .Start()
        End With
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        myRunningProcesses = Process.GetProcesses '// get current Processes.
        bAppFound = False
        For Each selectedProcess As Process In myRunningProcesses '// loop thru all Processes.
            If selectedProcess.ProcessName.ToLower = myAPPthatCrashes Then
                bAppFound = True
                Exit For '// exit loop since the app. is running.
            End If
        Next
        If bAppFound = False Then '// check if app. not found.
            For Each selectedProcess As Process In myRunningProcesses '// loop thru all Processes.
                If selectedProcess.ProcessName.ToLower = myKeyboardApp Then
                    selectedProcess.Kill() '// terminate the keyboard app..
                    Exit For '// exit loop since located and terminated.
                End If
            Next
        End If
    End Sub
End Class

I used Notepad as your app. that crashes and Calculator as the keyboard app..

commented: "Just Because" :) -2

codeorder, your code is for a background app or a service or something like that.
Something like this would never allow the Calculator in your example to run - for more than 1 sec - as a standalone app.
The problem is how to close the Calculator when the program that called it crashes.
I am assuming that rciprogrammer knows how to kill a process, but can't when the program crashes.

PS: rciprogrammer why is your program crashing? Not enough error handling?

Its not always crashing that was just an easy way to explain it. Most of the time the keyboard just doesn't close when the app closes and so its left running (which is a problem for security reasons).

Member Avatar for Unhnd_Exception

It seems like you could just put your code in the ApplicationEvents.


Go To Project / Properties / Application Tab and click the View Application Events Button. That will create an ApplicationEvent.vb file. You will have a Shutdown StartUp and UnhandledException event. I put code in the UnhandledException event for an optional send error report on crash. You could kill the other process there as well.

Go To Project / Properties / Application Tab and click the View Application Events Button. That will create an ApplicationEvent.vb file. You will have a Shutdown StartUp and UnhandledException event. I put code in the UnhandledException event for an optional send error report on crash. You could kill the other process there as well.

I just get a blank file. Should there be some default code in here?

Member Avatar for Unhnd_Exception

The application Event file should look like this.

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            Dim OutLookProcess As Process() = Process.GetProcessesByName("Outlook")

            For Each Process As Process In OutLookProcess
                Process.CloseMainWindow()
            Next
        End Sub

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Process.GetProcessesByName("Outlook").Count = 0 Then
                Process.Start("Outlook.exe")
            End If
        End Sub

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            Dim OutLookProcess As Process() = Process.GetProcessesByName("Outlook")

            For Each Process As Process In OutLookProcess
                Process.CloseMainWindow()
            Next
        End Sub

    End Class

End Namespace

I actually just did this today. I have a program that reads from my Outlook. This opens outlook when the program starts and closes it when it closes or crashes.

I don't deal with starting and stopping processes. If my code for starting and stopping process looks incorrect let me know. It works but not sure if there is a better way.

commented: Also "Just Because". :D -2

Thanks for the replies. Seems to work for most scenarios. What if somebody ends the process in the task manager? Is there a way to handle that?

The application Event file should look like this.

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            Dim OutLookProcess As Process() = Process.GetProcessesByName("Outlook")

            For Each Process As Process In OutLookProcess
                Process.CloseMainWindow()
            Next
        End Sub

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Process.GetProcessesByName("Outlook").Count = 0 Then
                Process.Start("Outlook.exe")
            End If
        End Sub

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            Dim OutLookProcess As Process() = Process.GetProcessesByName("Outlook")

            For Each Process As Process In OutLookProcess
                Process.CloseMainWindow()
            Next
        End Sub

    End Class

End Namespace

I actually just did this today. I have a program that reads from my Outlook. This opens outlook when the program starts and closes it when it closes or crashes.

I don't deal with starting and stopping processes. If my code for starting and stopping process looks incorrect let me know. It works but not sure if there is a better way.

This doesn't seem to work in Windows 7. Works fine in XP but nothing happens in 7. Any ideas why or what has to change?

Myprogram.exe could spawn osk.exe as well as an independent process (MyprogramMon.exe for example) that monitors Myprogram.exe. When the Myprogram.exe disappears, MyprogramMon.exe could terminate osk.exe then exit. This should work even if the main process crashes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.