<p>How can I bring my WPF application to the front of the desktop? So far I've tried:</p>

<pre><code>SwitchToThisWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle, true);

SetWindowPos(new WindowInteropHelper(Application.Current.MainWindow).Handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

SetForegroundWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle);
</code></pre>

<p>None of which are doing the job (<code>Marshal.GetLastWin32Error()</code> is saying these operations completed successfully, and the P/Invoke attributes for each definition do have <code>SetLastError=true</code>).</p>

<p>If I create a new blank WPF application, and call <code>SwitchToThisWindow</code> with a timer, it works exactly as expected, so I'm not sure why it's not working in my original case.</p>

<p><strong>Edit</strong>: I'm doing this in conjunction with a global hotkey.</p>

Well I figured out a work around. I'm making the call from a keyboard hook used to implement a hotkey. The call works as expected if I put it into a BackgroundWorker with a pause. It's a kludge, but I have no idea why it wasn't working originally.
```
```
void hotkey_execute()
{
    IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
    BackgroundWorker bg = new BackgroundWorker();
    bg.DoWork += new DoWorkEventHandler(delegate
        {
            Thread.Sleep(10);
            SwitchToThisWindow(handle, true);
        });
    bg.RunWorkerAsync();
}
```
```