dreadpirateryan
dreadpirateryan

Reputation: 545

Window for a newly started process in C# is showing up behind my current open windows

I have some fairly straightforward code to open up files using a Process object:

        var proc = new Process();
        proc.StartInfo.FileName = attachmentPath;
        proc.StartInfo.UseShellExecute = true;
        proc.Start();

Every once in a while, the window that opens from this process starting open behind all my currently open windows. I don't see any pattern or consistency to why this happens. Does anybody have an idea why this happens, or how I can work around it? Thanks!

Upvotes: 2

Views: 541

Answers (1)

Kevin Montrose
Kevin Montrose

Reputation: 22571

Windows has discouraged applications from stealing focus for a while; the rules around it aren't really documented, presumably to prevent applications from working around them.

However, you could give this a try:

    var proc = new Process();
    proc.StartInfo.FileName = attachmentPath;
    proc.StartInfo.UseShellExecute = true;
    proc.Start();

    //Wait for window to spin up
    proc.WaitForInputIdle();
    BringWindowToTop(proc.MainWindowHandle);

Define BringWindowToTop with p/invoke.

Upvotes: 1

Related Questions