Start new process, without being a child of the spawning process
Here is the code that I'm now using. I thought that it may be useful to someone. It accepts one argument. The argument is a base64 encoded string that decodes to the path of the file that you would like to run.
Module Module1
Sub Main()
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
If CommandLineArgs.Count = 1 Then
Try
Dim path As String = FromBase64(CommandLineArgs(0))
Diagnostics.Process.Start(path)
Catch
End Try
End
End If
End Sub
Function FromBase64(ByVal base64 As String) As String
Dim b As Byte() = Convert.FromBase64String(base64)
Return System.Text.Encoding.UTF8.GetString(b)
End Function
End Module
How to create a Process that outlives its parent
It seems that the problem you are seeing has a different reason because the Process
class will not kill any processes started using Process.Start
when your application exits.
See this simple sample program, the calculator will stay open:
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process.Start(@"C:\windows\system32\calc.exe");
}
}
Prevent Process 'A' from spawning Process 'B' which then shows up on top of what should be the TopMost Process 'C'
Setting the TopMost property (or adding the WS_EX_TOPMOST style to a window) does not make it unique in the system. Any number of topmost windows may be created by any number of applications; the only guarantee is that all topmost windows will be drawn 'above' all non-topmost windows. If there are two or more topmost windows, the Z-order still applies. From your description, I suspect that flash.exe is also creating a topmost window.
Aside from periodically forcing your window to the top of the Z-order, I think there is little you can do. Be warned, however, that this approach is dangerous: if two or more windows are simultaneously trying to force themselves to the top of the Z-order, the result will be a flickering mess that the user will likely have to use the task manager to escape.
I recommend that your program not attempt to meddle with other processes on the computer (unless that is its explicit purpose, e.g. a task manager clone). The computer belongs to the user, and he may not value your program more highly than all others.
Addendum:
For the emergency situation described in the comments, I would look at possible solutions along these lines:
How does the third party application normally get started and stopped? Am I permitted to close it the same way? If it is a service, the Service Control Manager can stop it. If it is a regular application, sending an escape keystroke (with SendInput() perhaps) or WM_CLOSE message to its main window may work.
If I can't close it nicely, am I permitted to kill it? If so, TerminateProcess() should work.
If I absolutely have to leave the other process running, I would try to see if I can programmatically invoke fast user switching to take me to a different session (in which there will be no competing topmost windows). I don't know where in the API to start with this one. (Peter Ruderman suggests SwitchDesktop() for this purpose in his answer.)
Related Topics
Launching a Process in User's Session from a Service
Get Error Message If Modelstate.Isvalid Fails
When Should One Use Dynamic Keyword in C# 4.0
Unzip a Memorystream (Containing the Zip File) and Get the Files
Event to Detect System Wake Up from Sleep in C#
List Sort Based on Another List
Reference Type Still Needs Pass by Ref
Cast Linq Result to Observablecollection
Best Way to Find All Factors of a Given Number
What Is the Correct Way to Read from Networkstream in .Net
How to Print Webview Content in a Windows Store App
Measure a String Without Using a Graphics Object
System.Web.Httpcontext.Current.User.Identity.Name VS System.Environment.Username in ASP.NET