---
title: "Open a second window in a C# desktop app"
url: https://recursiveai.org/oneapp/cookbook/open-second-window-in-desktop-app/
description: "Use OneApp to open two independent desktop windows from one C# app. Give each window its own title and page without starting another process."
version: "1.1.1"
---

The example opens two windows, Notebook and Preview. Call `host.OpenWindow` to put Preview in its own window. On desktop, OneApp runs on [Linux (Wayland and X11), macOS (Apple silicon), and Windows](https://github.com/recursive-ai-oss/oneapp-csharp/blob/b93a90dc510817e92a4ebd1e340b83118461665c/README.md#L62-L68).

Start from the zip, or run `dotnet new console` first.

```shell
dotnet add package RecursiveAI.OneApp
```

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

```csharp
using OneApp;

using var host = AppHost.Create(
    new AppOptions("<h1>Notebook</h1>")
    {
        Title = "Notebook",
        Width = 420, Height = 260
    });

var preview = host.OpenWindow(
    new AppWindowOptions("<h1>Preview</h1>")
    {
        Title = "Preview",
        Width = 320, Height = 200
    });

var count = host.Windows.Count;
Console.WriteLine($"Windows: {count}");
host.Run();
```

Close either window; the other stays open. The example exits when both are closed. Call `OpenWindow` on the UI thread; if a background operation needs to open a window, use `host.Post` to send the work to the UI thread instead.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/oneapp/cookbook/open-second-window-in-desktop-app/open-second-window-in-desktop-app.zip) and `dotnet run`.

Checked on Linux (X11): the app opens **Notebook** and **Preview** and prints:

```text
Windows: 2
```

## Variations

For a dialog attached to the first window, replace the `AppWindowOptions` in the `OpenWindow` call. `host` is the `AppHost` created in `Program.cs`:

```csharp
new AppWindowOptions("<h1>Preview</h1>")
{
    Title = "Preview",
    Width = 320, Height = 200,
    Parent = host.MainWindow
}
```

Checked on Linux (X11): closing Notebook ends the app even if the Preview dialog is open. The dialog isn’t a main window. Use an unparented window like the first example if you want Preview to keep the app open.

## Related recipes

- [Build a desktop app with HTML and CSS in C#](https://recursiveai.org/oneapp/cookbook/build-desktop-app-with-html-css/)
- [Open a native file dialog in a C# desktop app](https://recursiveai.org/oneapp/cookbook/open-native-file-picker/)

## The short answer

Call `host.OpenWindow(new AppWindowOptions(html))` on your existing `AppHost`. Omit `Parent` and `Overlay` to open an ordinary top-level window. Keep the returned `AppWindow` if you want to close it, change its title, or handle its `Closed` event later. Both windows use the same `host.Run()` loop.

## How do I open two windows of one app?

Create the host once. `AppHost.Create` creates the first window, and each call to `OpenWindow` creates another. Pass the new window its own HTML; for controls in that window, attach handlers in that window’s `Setup` callback. A preview can render text from the same notebook without sharing the editor’s page or controls.

The example opens the preview at startup, but an **Open Preview** button can call the same API from its click handler. Decide what another click means before adding that button. For a single preview, hold on to its handle and check `IsClosed` before creating another. For document windows that should coexist, keep one handle per document instead.

## Is a second window the same as a second app instance?

No. A second instance starts a separate process. A second window belongs to the existing app, so it can use the same in-memory data and event loop. Closing the first window also doesn’t tell an ordinary second window to close. The default policy, `LastMainWindowClosed`, ends the app when no main windows remain. If you want a tool window that doesn’t keep the app alive, set `IsMain` to `false` rather than assuming that “opened second” decides its lifetime.
