ApplicationInstance class

Namespace Runic.Platform.Runtime in Runic.Platform.Runtime 0.23.0

public static class ApplicationInstance

One running instance per application and user: a later launch hands its arguments to the running instance and exits. Works the same on Linux, Windows and macOS. On Windows the launching process also lets the running instance take the foreground (AllowSetForegroundWindow), so it can bring its window forward.

Remarks

The running instance holds an exclusive lock file, which the operating system releases when the process ends, and listens on a Unix domain socket in a directory only the user can use: XDG_RUNTIME_DIR on Linux (the application's directory inside a Flatpak sandbox), the per-user temporary directory on macOS, and local application data on Windows.

With ApplicationInstanceOptions.ReplyToLaunches a later launch also waits for the running instance's reply, a stream of messages and an exit status, over the same socket: a command line can hand its work to the running instance and report the outcome. ApplicationLaunch.Reply is the running instance's side, Forwarded.Reply the launching process's.

Methods

ClaimAsync

public static ValueTask<PlatformResult<ApplicationInstanceClaim>> ClaimAsync(string applicationId, ApplicationLaunch launch, CancellationToken cancellationToken = default)

Becomes the running instance of applicationId, or forwards launch to it.

Parameters

applicationId

The application's reverse-DNS ID, such as org.example.App.

launch

This process's launch, usually ApplicationLaunch.FromProcess.

cancellationToken

Cancels waiting for the running instance.

Returns

Primary or Forwarded; unavailable without a per-user runtime directory or when its path is too long for a socket, and PlatformFailureCode.ResourceBusy when another instance holds the lock but does not accept the launch. The application decides whether to run anyway.

ClaimAsync

public static ValueTask<PlatformResult<ApplicationInstanceClaim>> ClaimAsync(string applicationId, ApplicationLaunch launch, ApplicationInstanceOptions options, CancellationToken cancellationToken = default)

Becomes the running instance of applicationId, or forwards launch to it, as options describe.

Parameters

applicationId

The application's reverse-DNS ID, such as org.example.App.

launch

This process's launch, usually ApplicationLaunch.FromProcess.

options

Whether the launches of this application are answered.

cancellationToken

Cancels waiting for the running instance.

Returns

As ApplicationInstance.ClaimAsync. With ApplicationInstanceOptions.ReplyToLaunches, Forwarded.Reply is set, and a running instance that does not reply is PlatformFailureCode.ResourceBusy with an ApplicationInstance diagnostic whose code is the reply protocol version that instance supports, 0 for none; the launch was not handled. With ApplicationInstanceOptions.ForwardOnly, NotRunning when no instance runs, instead of Primary.

ClaimAsync

public static ValueTask<PlatformResult<ApplicationInstanceClaim>> ClaimAsync(string applicationId, string[] args, CancellationToken cancellationToken = default)

Becomes the running instance of applicationId, or forwards this process's launch with args to it.

Parameters

applicationId

The application's reverse-DNS ID, such as org.example.App.

args

The process's command-line arguments, as Main receives them.

cancellationToken

Cancels waiting for the running instance.

Returns

As ApplicationInstance.ClaimAsync.

Remarks

Equivalent to ApplicationInstance.ClaimAsync with ApplicationLaunch.FromProcess(args).

In an MSIX package, Windows hands a protocol activation or the files of a file-type activation to the process through its activation, not args, so this overload forwards none of them. Pass DesktopPlatformServices.GetLaunch(args) (Runic.Application.Views.Desktop) or WindowsPlatformProvider.GetLaunch(args) (Runic.Platform.Windows) to the overload that takes an ApplicationLaunch instead; both return ApplicationLaunch.FromProcess(args) outside a package.

Example

var claim = await ApplicationInstance.ClaimAsync("org.example.App", args);

ClaimAsync

public static ValueTask<PlatformResult<ApplicationInstanceClaim>> ClaimAsync(string applicationId, string[] args, ApplicationInstanceOptions options, CancellationToken cancellationToken = default)

Becomes the running instance of applicationId, or forwards this process's launch with args to it, as options describe.

Parameters

applicationId

The application's reverse-DNS ID, such as org.example.App.

args

The process's command-line arguments, as Main receives them.

options

Whether the launches of this application are answered.

cancellationToken

Cancels waiting for the running instance.

Returns

As ApplicationInstance.ClaimAsync.

Remarks

Equivalent to ApplicationInstance.ClaimAsync with ApplicationLaunch.FromProcess(args).

In an MSIX package, Windows hands a protocol activation or the files of a file-type activation to the process through its activation, not args, so this overload forwards none of them. Pass DesktopPlatformServices.GetLaunch(args) (Runic.Application.Views.Desktop) or WindowsPlatformProvider.GetLaunch(args) (Runic.Platform.Windows) to the overload that takes an ApplicationLaunch instead; both return ApplicationLaunch.FromProcess(args) outside a package.

CreateOpenRequests

public static IDesktopOpenRequests CreateOpenRequests(DesktopOpenOptions options, IApplicationInstance? instance = null, ApplicationLaunch? launch = null)

Delivers the files and URLs of launch and of each later launch that instance receives, as Linux and Windows pass them: as arguments.

Parameters

options

The URL schemes and files the application registered.

instance

The running instance, or null without single instance.

launch

This process's launch, usually ApplicationLaunch.FromProcess; null delivers only later launches.

Returns

Open requests to dispose when the application ends; disposal stops listening to the instance.

Exceptions

ArgumentException

options names a string that is not a URL scheme, or file.

Remarks

Each launch becomes a request through DesktopOpenRequests.FromLaunch; a launch without files or registered URLs raises nothing. The instance's own IApplicationInstance.Launched handlers still receive every launch, and complete ApplicationLaunch.Reply where one is set. On macOS the system sends open events instead; MacOSPlatformProvider.CreateOpenRequests handles both.