4

When I run another .exe from my application it starts in the background and does not show the application on top of the screen instead shows tablet mode home screen , it's working fine in normal desktop mode but when I run it in Windows 10 Tablet mode then it does not show on top it starts in the background.

I've used myWindow.TopMost = true; , but it does not work as intended in Windows 10 Tablet Mode.

Code used to start exe file

Process p = new Process();
p.StartInfo.RedirectStandardOutput= true;
p.RedirectStandardInput = true;
p = Process.Start("myApp.exe");
p.WaitForExit();

the exe I'm invoking(starting) is my own exe application(it's not system app), I'm running app on windows 10.

It's only not working on top in Tablet mode( and I'm targeting my application only for Tablets).

Any help is appreciated..!

H.B.
  • 166,899
  • 29
  • 327
  • 400
Rahul Shirphule
  • 989
  • 3
  • 14
  • 36
  • you should set the parent window which hadles your application. Without setting that, the `TopMost = true` will not help you. – mihai Aug 03 '16 at 12:49

2 Answers2

3

As I faced a similar situation, (it is not tablet, or windows-10 related. Has similarities only by WPF and TopMost tags) I'll show you how I resolve it: I would like to have the FilterWindow always TopMost (but only over my application, not over entire set of apps in my Operating System)

See my code. May it'll helps you.

private void OnFilter() {   
    var filterViewModel = ViewModelLocator.FilterViewModel;

    /* ... */

    var filterWindow = new FilterWindow {
        DataContext = filterViewModel,
        Owner = GetParentWindow()
    };
    filterWindow.ShowDialog();
    SelectedIndex = 0;
}

private static Window GetParentWindow() {
    Window parent = null;

    var activeWindows = Application.Current.Windows.Cast<Window>().Where(item => (item).IsActive).ToList();
    if (activeWindows.Any()) {
    parent = activeWindows[activeWindows.Count - 1];
    }
    else {
        foreach (var item in 
            Application.Current.Windows.Cast<object>().Where(item => item.GetType().Name == typeof(RibbonWindow).Name)) {
            parent = item as Window;
        }
    }
    return parent;
}

The magic is Owner = GetParentWindow().
Without setting the Owner the FilterWindow had a ridiculous behavior.

Hope it helps you. If no, I will remove the response. (it does not fit in a comment)

mihai
  • 2,746
  • 3
  • 35
  • 56
  • Thanks for response, But Where should I write this code because I'm staring new exe (app). As I posted my code to call exe from Process.Start()..?? – Rahul Shirphule Aug 03 '16 at 13:26
  • @RahulShirphule, In case of Processes, you should deal with Window Functions, from WinApi. `SetParent` should do the trick (when you start the EXE) See more : https://msdn.microsoft.com/en-us/library/windows/desktop/ms633541(v=vs.85).aspx . Also, this can be interesting for you too: http://stackoverflow.com/questions/8312535/can-you-launch-a-process-on-top-of-the-topmost-window-csharp-wpf – mihai Aug 04 '16 at 06:42
  • Yes I gone through this link..but when to call that setParent Method is not mentioned , means before or after p = Process.Start("myApp.exe"); p.WaitForExit(); I tried both with no luck...do you have any idea..? please let me know? – Rahul Shirphule Aug 04 '16 at 06:48
  • @RahulShirphule, i see here (attached url), SetParent is called after the process is started. http://stackoverflow.com/questions/10434658/how-to-open-process-within-windows-form . Also, here: http://stackoverflow.com/questions/15393707/running-exe-inside-winform-project-tab – mihai Aug 04 '16 at 13:11
0

Moerfi's solution of using Owner = GetParentWindow() worked surperb, much thanks for this solution. It also solved another problem I had.

I am writing an application for Surface 3 that runs on Windows 10 Pro on tablet mode, whenever a MessageBox or custom dialog control box is closed, as opposed to going back to parent window, Win 10 goes to start menu.

As if once the dialog control is opened the parent window is being placed into background, so when dialog control is closed there is no active window for Win 10 to switch back.

Setting owner on child dialog control solved that problem. Thank you very much.