Show / Hide Table of Contents

Initializing a UI application

To create a UI application, you need to:

  • Create an instance of the TTAPIOptions class passing the TT environment to which your application will connect, your application key for this environment, and a timeout interval.

    TT recommends setting the value of the Timeout interval to 5000 milliseconds.

  • Attach a UI dispatcher to the UI thread on which you will consume events and start it.

  • Initialize TT .NET SDK.

  • Authenticate your credentials.

Client-side mode example

The following code snippet demonstrates an example of this process for client-side mode.

// ...

using tt_net_sdk;

// ...

static class Program
{
  [STAThread]
  static void Main()
  {
        using (Dispatcher disp = Dispatcher.AttachUIDispatcher())
        {
            Application.EnableVisualStyles();
            
            // Create an instance of the API
            MyApp myApp = new MyApp();

            // Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
            string appSecretKey = "you app key here";

            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;
            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(environment, appSecretKey, 5000);

            apiConfig.BinaryProtocol = true;

            ApiInitializeHandler handler = new ApiInitializeHandler(myApp.ttNetApiInitHandler);
            TTAPI.CreateTTAPI(disp, apiConfig, handler);

            Application.Run(myApp);
        }
  }
}


// ...

using tt_net_sdk;

// ...

public partial class MyApp : Form
{
    // Declare the API objects
    private TTAPI m_api = null;
    private bool m_isShutdown = false, m_shutdownInProcess = false;


    public MyApp()
    {
        InitializeComponent();
    }

    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
    {
        if (ex == null)
        {
            // TT NET SDK INITIALIZED
            m_api = api;
            m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
            m_api.Start();
        }
        else if (ex.IsRecoverable)
        {
            // Initialization failed but retry is in progress...
        }
        else
        {
            MessageBox.Show("API Initialization Failed: " + ex.Message);
        }
    }

    private void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)
    {
        if (e.IsReady)
        {
            // connection to TT is established
        }
        else if (ex.IsDown)
        {
            // connection is down but TT .NET SDK will automatically attempt to reconnect 
        }
        else
        {
            MessageBox.Show(String.Format("M_TTAPI_TTAPIStatusUpdate: {0}", e));
        }
    }

    public void shutdownTTAPI()
    {
        if (!m_shutdownInProcess)
        {
            TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
            TTAPI.Shutdown();

            m_shutdownInProcess = true;
        }
    }

    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)
    {
        m_isShutdown = true;
        Close();
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (!m_isShutdown)
        {
            e.Cancel = true;
            shutdownTTAPI();
        }
        else
        {
            base.OnFormClosing(e);
        }
    }
}

Server-side mode example

The following code snippet demonstrates an example of this process for server-side mode.

// ...

using tt_net_sdk;

// ...

static class Program
{
  [STAThread]
  static void Main()
  {
        using (Dispatcher disp = Dispatcher.AttachUIDispatcher())
        {
            Application.EnableVisualStyles();
            
            // Create an instance of the API
            MyApp myApp = new MyApp();

            // Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
            string appSecretKey = "you app key here";

            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;
            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(
                    tt_net_sdk.TTAPIOptions.SDKMode.Server, environment, appSecretKey, 5000);
            apiConfig.ServerInstanceId = “<InstanceIdAssignedToYourMachineByTT>”;

            ApiInitializeHandler handler = new ApiInitializeHandler(myApp.ttNetApiInitHandler);
            TTAPI.CreateTTAPI(disp, apiConfig, handler);

            Application.Run(myApp);
        }
  }
}


// ...

using tt_net_sdk;

// ...

public partial class MyApp : Form
{
    // Declare the API objects
    private TTAPI m_api = null;
    private bool m_isShutdown = false, m_shutdownInProcess = false;


    public MyApp()
    {
        InitializeComponent();
    }

    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
    {
        if (ex == null)
        {
            Console.WriteLine("TT.NET SDK Initialization Complete");
            // Authenticate your credentials
            m_api = api;
            m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
            m_api.Start();
        }
        else if (ex.IsRecoverable)
        {
            // this is in informational update from the SDK
            Console.WriteLine("TT.NET SDK Initialization Message: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionAvailable)
            {
                // a newer version of the SDK is available - notify someone to upgrade
            }
        }
        else
        {
            Console.WriteLine("TT.NET SDK Initialization Failed: {0}", ex.Message);
            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionRequired)
            {
                // do something to upgrade the SDK package since it will not start until it is upgraded 
                // to the minimum version noted in the exception message
            }
            Dispose();
        }
    }

    private void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)
    {
        if (e.IsReady)
        {
            // connection to TT is established
        }
        else if (ex.IsDown)
        {
            // connection is down but TT .NET SDK will automatically attempt to reconnect 
        }
        else
        {
            MessageBox.Show(String.Format("M_TTAPI_TTAPIStatusUpdate: {0}", e));
        }
    }

    public void shutdownTTAPI()
    {
        if (!m_shutdownInProcess)
        {
            TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
            TTAPI.Shutdown();

            m_shutdownInProcess = true;
        }
    }

    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)
    {
        m_isShutdown = true;
        Close();
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (!m_isShutdown)
        {
            e.Cancel = true;
            shutdownTTAPI();
        }
        else
        {
            base.OnFormClosing(e);
        }
    }
}
In this article
Back to top