Loading...
Searching...
No Matches
sharedmemory.cs

This example can be found in `examples\csharp\sharedmemory\sharedmemory.cs`. When the PST SDK has been installed through the PST Software Suite installer the examples can be found in the Development folder.

This example shows how to activate the shared memory communication pipeline that enables communication of the PST Client application through the PST SDK. Note that for the shared memory pipeline to function, the application has to run with elevated access (administrator rights). After enabling shared memory, the PST Client application can be used to download calibration information and manage tracking targets.

When compiling and running this example, please make sure that the required dependencies can be found by the executable (e.g. by copying the `Redist` directory into the build directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder.).

// Copyright PS-Tech B.V. All Rights Reserved.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using PSTech.Pstsdk;
namespace PSTech.SharedMemory
{
public class SharedMemory
{
//Registering control handler function.
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler handler, bool add);
private delegate bool ConsoleEventHandler(CtrlType sig);
static ConsoleEventHandler ConsoleCtrlEventHandler;
public enum CtrlType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogOffEvent = 5,
CtrlShutdownEvent = 6,
}
private static bool ConsoleHandler(CtrlType sig)
{
Exithandler((int)sig);
return true;
}
static void Exithandler(int sig)
{
Tracker.DisableSharedMemory();
Tracker.Shutdown(); // Cleans up native resources only; does NOT remove listeners
Environment.Exit(0);
}
static int Main(string[] args)
{
// Register the exit handler with the application
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
// Implement error handling of PSTech.TrackerException exceptions to prevent
// improper PST Tracker shutdown on errors.
try
{
// Create an instance of the Tracker object using the default configuration path and file names.
// Wrap the Tracker in a using statement to ensure Dispose() is called automatically
using (Tracker tracker = new Tracker())
{
// Start the tracker server.
tracker.StartTracker();
// Enable the shared memory pipeline to enable client connections.
Tracker.EnableSharedMemory();
Console.Write("Shared memory server initialized. Start the PST Client application to create a connection.\n");
// Wait for one minute before terminating this application.
Thread.Sleep(60000);
// Disable the shared memory pipeline. If the PST Client is still connected at this point,
// it will try to reconnect and otherwise exit.
Tracker.DisableSharedMemory();
}
}
catch (Exception e)
{
Console.Write(e + "\n");
// Handle the error.
}
return 0;
}
}
}
PSTech.Pstsdk.Tracker
Main PST SDK class implementing tracker communication.
Definition Tracker.cs:198
PSTech.Pstsdk.Tracker.Shutdown
static void Shutdown()
Shutdown the tracking system, stopping tracking.
Definition Tracker.cs:548
PSTech.Pstsdk.Tracker.StartTracker
void StartTracker()
Start tracking.
Definition Tracker.cs:524
PSTech.Pstsdk.Tracker.DisableSharedMemory
static void DisableSharedMemory()
Disable shared memory communication layer.
Definition Tracker.cs:1013
PSTech.Pstsdk
Definition CApi.cs:9