---
title: "Bring an existing native window to the foreground in C#"
url: https://recursiveai.org/oneplatform/cookbook/focus-an-existing-native-window/
description: "Ask the desktop to raise, restore, and focus a window your C# app already owns. Use OnePlatform activation without making the window always on top."
version: "1.1.0"
---

# Bring an existing native window to the foreground in C#

**OnePlatform** is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/focus-an-existing-native-window/focus-an-existing-native-window.zip)

Call `Activate()` on a window your app owns. It isn’t an API for focusing an arbitrary window belonging to another process. It asks the desktop to bring it forward and send keyboard input to it.

```shell
dotnet add package RecursiveAI.OnePlatform
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePlatform;
using Kind = OnePlatform.PlatformEventKind;

using var app = PlatformApp.Create();
var target = app.CreateWindow(new()
{
    Title = "Document"
});
var control = app.CreateWindow(new()
{
    Title = "Click to focus Document"
});

while (true)
{
    app.PumpEvents(100);
    while (app.TryGetEvent(out var e))
    {
        if (e.Kind is Kind.WindowCloseRequested
            or Kind.QuitRequested)
            return;

        if (e.WindowId == control.Id &&
            e.Kind == Kind.PointerButtonDown &&
            e.Button == MouseButton.Left)
        {
            target.Activate();
            Console.WriteLine(
                "Activation requested.");
        }

        if (e.WindowId == target.Id &&
            e.Kind == Kind.WindowFocusChanged &&
            e.Focused)
            Console.WriteLine(
                "Document has focus.");
    }
}
```

The desktop can refuse an activation request. An unsupported window or backend can throw `ActivationUnavailableException`; on Wayland, for example, the compositor may lack `xdg_activation_v1`.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/oneplatform/cookbook/focus-an-existing-native-window/focus-an-existing-native-window.zip) and `dotnet run`.

Click inside **Click to focus Document**. Click either window’s close button (×) to quit. On Linux (X11), the example printed these lines when the click brought **Document** forward:

```text
Activation requested.
Document has focus.
```

## Variations

When handling a launcher action rather than a click, pass along the Wayland token supplied for that launch. Here `target` is the same existing window from the example:

```csharp
var token = Environment.GetEnvironmentVariable(
    "XDG_ACTIVATION_TOKEN");
target.Activate(token);
```

## Related recipes

- [Create a native desktop window](https://recursiveai.org/oneplatform/cookbook/create-native-cross-platform-window/)
- [Create an always-on-top overlay](https://recursiveai.org/oneplatform/cookbook/create-always-on-top-overlay/)

## The short answer

Call `target.Activate()` on the UI thread, where `target` is an open OnePlatform window you already created. The call asks the desktop to raise the window, restore it if minimized, and give it keyboard focus. Run it from a user action such as a click.

## How do you bring a C# window to the front without keeping it on top?

Activation is a one-time request. After it succeeds, the user can switch to another window as usual. `SetKeepAbove(true)` changes stacking policy instead: it keeps an ordinary window above others when the backend supports that option. That’s not needed to return to a document or open a window the user selected from a list. Keep a reference to the existing window and request activation rather than creating a second copy.

A return from `Activate` doesn’t mean the target has the keyboard. If you need to know when it’s ready for input, watch for `WindowFocusChanged` with `Focused` set and the target’s `WindowId`, as the example does. The example logs focus changes caused by the desktop as well as changes from the click; you may see a focus line during startup.

## Why doesn’t a request to bring the window forward always work?

The window system decides whether the request is backed by user input. On X11, OnePlatform sends the window manager the time of the latest key or button press. On Wayland it can use a launch token, or ask for one tied to recent input. Even with support for activation, a Wayland compositor can decline the request. It may show a taskbar hint instead. Windows also restricts which processes can claim the foreground.

If a running app gets a launch request from a second instance, OnePlatform’s `SingleInstance` can forward a launcher token. The running instance can pass `ActivationRequest.ActivationToken` to `Activate` instead of relying on an earlier click. The [desktop window docs](https://recursiveai.org/oneplatform/docs/desktop-windows/#bringing-a-window-forward) describe what each backend accepts.
