I'm trying to open a file through my app, and monitor when the users closes the app. I'm using the below to open a file, and monitor when it closes:
Dim NewProcess As New Process
NewProcess.EnableRaisingEvents = True
AddHandler NewProcess.Exited, AddressOf Process_Exited
NewProcess.StartInfo.FileName = "c:\test.txt"
NewProcess.Start()
Private Sub Process_Exited(sender As Object, e As EventArgs)
Console.WriteLine("App Exited @ " & DateTime.Now)
End Sub
The above works properly for eg txt files opened in notepad. But when trying for other kind of files, eg. a .png file, the process is started and disposed immediately, while not even triggering the Process_Exited sub. As far as I can tell, it calls another process (which opens the file) and kills itself immediately. The new process is not a child process of the original, it's an entirely separate process.
How can I get the new process(processes) and check if they have closed? I would like to avoid getting a collection of processes before and after calling process.start, I think that it will not work properly if a random other process starts at the exact same time.
A workaround I tried was to open the application directly, using process.start(pathtoexe, pathtofile), but this can be problematic on its own, as I have to manually read the associated application from the registry, separate any default arguments the shell may be passing to the application, and inject them in front of the path to the file, hoping that the application accepts a filepath as a parameter.
Any suggestions? Thanks in advance