Server.CreateSessionAsync
Creates a session.
Source and package
- CreateSessionAsync ( request : New
Session ? , cancellationToken : CancellationRequest Token ) Task<Session> -
Description#
Set
ExpectedGenerationto require one daemon before creation. Readback always uses the generation reported by creation itself. A daemon change after creation reports a partial failure rather than returning its replacement.Examples#
Create entities directly inside an owned server scope, then read the session and window children. The outer scope stops its daemon and all created entities. Use Linux or macOS with Git, tmux 3.2a or newer, .NET SDK 10.0.303, and the .NET 8 and 10 runtimes. In an empty directory, fetch the documented library revision:
Terminal window $ git clone https://github.com/libtmux/libtmux-dotnet.git libtmux-source && \git -C libtmux-source checkout --detach cf34b255c896d19a97e7d7fabea58609c916012dSave this complete consumer setup as global.json: View source
{"sdk": {"version": "10.0.303","rollForward": "disable","allowPrerelease": false},"test": {"runner": "Microsoft.Testing.Platform"}}Save this complete consumer setup as NuGet.config: View source
<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><clear /><add key="libtmux-source" value="libtmux-source/artifacts/api-example-packages" /><add key="nuget.org" value="https://api.nuget.org/v3/index.json" /></packageSources><packageSourceMapping><clear /><packageSource key="libtmux-source"><package pattern="LibTmux" /><package pattern="LibTmux.*" /></packageSource><packageSource key="nuget.org"><package pattern="*" /></packageSource></packageSourceMapping></configuration>Save this complete consumer setup as Example.csproj: View source
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFrameworks>net8.0;net10.0</TargetFrameworks><LangVersion>12.0</LangVersion><Nullable>enable</Nullable><ImplicitUsings>disable</ImplicitUsings><EnableDefaultCompileItems>false</EnableDefaultCompileItems><TreatWarningsAsErrors>true</TreatWarningsAsErrors><AnalysisLevel>10-recommended</AnalysisLevel></PropertyGroup><ItemGroup><Compile Include="Program.cs" /><PackageReference Include="LibTmux" Version="[0.0.0-alpha.20]" /></ItemGroup></Project>Save this complete program as Program.cs. It creates a private server; await using disposes its owned scopes after success, cancellation or failure. The complete file contains explicit imports, a top-level async entry point and a cancellation deadline. View source
using System;using System.Collections.Generic;using System.Linq;using System.Threading;using LibTmux;if (OperatingSystem.IsWindows()){throw new PlatformNotSupportedException("This example requires tmux on Linux or macOS.");}using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(10));CancellationToken token = deadline.Token;string binary = Environment.GetEnvironmentVariable("LIBTMUX_TMUX") ?? "tmux";string socketName = "csharp-createwindowpane-" + Guid.NewGuid().ToString("N");ServerConnectionOptions options = new(){SocketName = socketName,ConfigurationFile = "/dev/null",TmuxBinaryPath = binary,};// The outer server scope owns everything created directly inside this daemon.await using OwnedServerScope owned = await Server.CreateOwnedAsync(options, token);Session session = await owned.Value.CreateSessionAsync(new NewSessionRequest { Name = "demo", WindowName = "shell", Command = "/bin/cat" }, token);Window window = await session.CreateWindowAsync(new NewWindowRequest { Name = "editor", Command = "/bin/cat" }, token);Pane original = (await window.GetPanesAsync(token)).Single();Pane added = await original.SplitAsync(new SplitPaneRequest { Direction = PaneDirection.Right, Command = "/bin/cat" }, token);IReadOnlyList<Window> windows = await session.GetWindowsAsync(token);IReadOnlyList<Pane> panes = await session.GetPanesAsync(token);if (windows.Count != 2 || panes.Count != 3 || added.Id == original.Id){throw new InvalidOperationException("Expected two windows and three distinct panes.");}Console.WriteLine($"Created session {session.Name} and window {window.Name}.");Console.WriteLine($"Windows: {windows.Count}; panes: {panes.Count}");Build the exact source package and the saved consumer, then run both supported frameworks. The local package feed prevents an unrelated published package from satisfying the restore. Each program checks its result and exits unsuccessfully after an unhandled error:
Terminal window $ export NUGET_PACKAGES="$PWD/packages" && \dotnet restore libtmux-source/src/LibTmux/LibTmux.csproj --locked-mode && \dotnet pack libtmux-source/src/LibTmux/LibTmux.csproj \--configuration Release --no-restore -p:ContinuousIntegrationBuild=true \--output "$PWD/libtmux-source/artifacts/api-example-packages" && \dotnet restore Example.csproj --configfile NuGet.config && \dotnet build Example.csproj --configuration Release --no-restore --warnaserror && \export LIBTMUX_TMUX="$(command -v tmux)" && \export TMUX_TMPDIR=/tmp/libtmux-dotnet-dev && \mkdir -p "$TMUX_TMPDIR" && \unset TMUX TMUX_PANE && \dotnet bin/Release/net8.0/Example.dll && \dotnet bin/Release/net10.0/Example.dllExpected program output from each framework:
Created session demo and window editor.Windows: 2; panes: 3- Parameters
-
-
request ( New
Session ? ) – The session to create.Request -
cancellationToken ( Cancellation
Token ) – Cancels the tmux command.
-
- Returns
-
The created session.
- Raises
-
-
Tmux
Session – The name is already taken.Exists Exception -
Argument
Exception – The name is invalid, or generation-bound creation requests replacement.
-
tmux command
new-sessionIn other ports Create a session
- Python
libtmux.Server.new_session - Ruby
LibTmux::Server#new_session - Lua
libtmux.Server:new_session - TypeScript
server.Server.newSession - Rust
server.Server.new_session - Go
tmux.Server.NewSession - Java
io.github.libtmux.Server.newSession - C++
libtmux::Server::new_session - Swift
Server.newSession(named:startDirectory:windowName:width:height:environment:shell:) - Kotlin
io.github.libtmux.kotlin.Server.newSession - Scala
io.github.libtmux.scaladsl.Server.newSession - F# No source-verified F# equivalent is recorded for this operation.