{"id":8041,"date":"2025-09-04T23:22:33","date_gmt":"2025-09-05T04:22:33","guid":{"rendered":"https:\/\/librarytestdev.wpenginepowered.com\/?post_type=doc&#038;p=8041"},"modified":"2025-09-04T23:23:55","modified_gmt":"2025-09-05T04:23:55","slug":"initializing-a-console-application","status":"publish","type":"doc","link":"https:\/\/library-staging.tradingtechnologies.com\/apis\/tt-net-sdk\/creating-the-application-framework\/initializing-a-console-application\/","title":{"rendered":"Initializing a console application"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To create a Console application, you need to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create an instance of the\u00a0<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.TTAPIOptions.html\">TTAPIOptions<\/a>\u00a0class passing the TT environment to which your application will connect, your\u00a0<a href=\"\/tt-net-sdk\/articles\/af-app-key.html\">application key<\/a>\u00a0for this environment, and a timeout interval.TT recommends setting the value of the Timeout interval to 5000 milliseconds.<\/li>\n\n\n\n<li>Attach a worker dispatcher to the thread on which you will consume events and start it.<\/li>\n\n\n\n<li>Initialize TT .NET SDK.<\/li>\n\n\n\n<li>Authenticate your credentials.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"client-side-mode-example\"><a href=\"\/tt-net-sdk\/articles\/af-console-app.html#client-side-mode-example\"><\/a>Client-side mode example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following code snippet demonstrates an example of this process for client-side mode.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ...\n\nusing tt_net_sdk;\n\n\/\/ ...\n\nclass Program\n{\n    static void Main(string&#91;] args)\n    {\n        try\n        {\n            \/\/ Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000\n            string appSecretKey = \"Your App Key\";\n\n            \/\/Set the environment the app needs to run in here\n            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;\n\n            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(\n                     environment,\n                     appSecretKey,\n                     5000);\n\n            apiConfig.BinaryProtocol = true;\n\n            \/\/ Start the TT API on the same thread\n            TTNetApiFunctions tf = new TTNetApiFunctions();\n\n            Thread workerThread = new Thread(() =&gt; tf.Start(apiConfig));\n            workerThread.Name = \"TT NET SDK Thread\";\n            workerThread.Start();\n\n            while (true)\n            {\n                string input = System.Console.ReadLine();\n                if (input == \"q\")\n                    break;\n            }\n            tf.Dispose();\n        }\n        catch (Exception e)\n        {\n            Console.WriteLine(e.Message + \"\\n\" + e.StackTrace);\n        }\n    }\n}\n\n\n\/\/ ...\n\nusing tt_net_sdk;\n\n\/\/ ...\n\npublic class TTNetApiFunctions\n{\n    \/\/ Declare the API objects\n    private TTAPI m_api = null;\n    private tt_net_sdk.WorkerDispatcher m_disp = null;\n\n\n    public void Start(tt_net_sdk.TTAPIOptions apiConfig)\n    {\n        m_disp = tt_net_sdk.Dispatcher.AttachWorkerDispatcher();\n        m_disp.DispatchAction(() =&gt;\n             {\n                 Init(apiConfig);\n             });\n\n        m_disp.Run();\n    }\n\n    public void Init(tt_net_sdk.TTAPIOptions apiConfig)\n    {\n        ApiInitializeHandler apiInitializeHandler = new ApiInitializeHandler(ttNetApiInitHandler);\n        TTAPI.CreateTTAPI(tt_net_sdk.Dispatcher.Current, apiConfig, apiInitializeHandler);\n    }\n\n    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)\n    {\n        if (ex == null)\n        {\n            Console.WriteLine(\"TT.NET SDK Initialization Complete\");\n            \/\/ Authenticate your credentials\n            m_api = api;\n            m_api.TTAPIStatusUpdate += new EventHandler&lt;TTAPIStatusUpdateEventArgs&gt;(m_api_TTAPIStatusUpdate);\n            m_api.Start();\n        }\n        else if (ex.IsRecoverable)\n        {\n            \/\/ this is in informational update from the SDK\n            Console.WriteLine(\"TT.NET SDK Initialization Message: {0}\", ex.Message);\n            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionAvailable)\n            {\n                \/\/ a newer version of the SDK is available - notify someone to upgrade\n            }\n        }\n        else\n        {\n            Console.WriteLine(\"TT.NET SDK Initialization Failed: {0}\", ex.Message);\n            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionRequired)\n            {\n                \/\/ do something to upgrade the SDK package since it will not start until it is upgraded \n                \/\/ to the minimum version noted in the exception message\n            }\n            Dispose();\n        }\n    }\n\n    public void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)\n    {\n        if (e.IsReady)\n        {\n            \/\/ connection to TT is established\n            Console.WriteLine(\"TT.NET SDK Authenticated\");\n        }\n        else if (ex.IsDown)\n        {\n            \/\/ connection is down but TT .NET SDK will automatically attempt to reconnect\n            Console.WriteLine(\"Connection down. Reconnecting...\");\n        }\n        else\n        {\n            Console.WriteLine(\"TT.NET SDK Status: {0}\", e);\n        }\n\n    }\n\n    public void Dispose()\n    {\n        TTAPI.ShutdownCompleted += TTAPI_ShutdownCompleted;\n        TTAPI.Shutdown();\n    }\n\n    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)\n    {\n        Console.WriteLine(\"TTAPI Shutdown completed\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"server-side-mode-example\"><a href=\"\/tt-net-sdk\/articles\/af-console-app.html#server-side-mode-example\"><\/a>Server-side mode example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following code snippet demonstrates an example of this process for server-side mode.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ...\n\nusing tt_net_sdk;\n\n\/\/ ...\n\nclass Program\n{\n    static void Main(string&#91;] args)\n    {\n        try\n        {\n            \/\/ Add your app secret Key here. It looks like: 00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000\n            string appSecretKey = \"Your App Key\";\n\n            \/\/Set the environment the app needs to run in here\n            tt_net_sdk.ServiceEnvironment environment = tt_net_sdk.ServiceEnvironment.UatCert;\n\n            tt_net_sdk.TTAPIOptions apiConfig = new tt_net_sdk.TTAPIOptions(\n                    tt_net_sdk.TTAPIOptions.SDKMode.Server,\n                    environment,\n                    appSecretKey,\n                    5000);\n                    \n            apiConfig.ServerInstanceId = \u201c&lt;InstanceIdAssignedToYourMachineByTT&gt;\u201d;\n\n            \/\/ Start the TT API on the same thread\n            TTNetApiFunctions tf = new TTNetApiFunctions();\n\n            Thread workerThread = new Thread(() =&gt; tf.Start(apiConfig));\n            workerThread.Name = \"TT NET SDK Thread\";\n            workerThread.Start();\n\n            while (true)\n            {\n                string input = System.Console.ReadLine();\n                if (input == \"q\")\n                    break;\n            }\n            tf.Dispose();\n        }\n        catch (Exception e)\n        {\n            Console.WriteLine(e.Message + \"\\n\" + e.StackTrace);\n        }\n    }\n}\n\n\n\/\/ ...\n\nusing tt_net_sdk;\n\n\/\/ ...\n\npublic class TTNetApiFunctions\n{\n    \/\/ Declare the API objects\n    private TTAPI m_api = null;\n    private tt_net_sdk.WorkerDispatcher m_disp = null;\n\n\n    public void Start(tt_net_sdk.TTAPIOptions apiConfig)\n    {\n        m_disp = tt_net_sdk.Dispatcher.AttachWorkerDispatcher();\n        m_disp.DispatchAction(() =&gt;\n             {\n                 Init(apiConfig);\n             });\n\n        m_disp.Run();\n    }\n\n    public void Init(tt_net_sdk.TTAPIOptions apiConfig)\n    {\n        ApiInitializeHandler apiInitializeHandler = new ApiInitializeHandler(ttNetApiInitHandler);\n        TTAPI.CreateTTAPI(tt_net_sdk.Dispatcher.Current, apiConfig, apiInitializeHandler);\n    }\n\n    public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)\n    {\n        if (ex == null)\n        {\n            Console.WriteLine(\"TT.NET SDK Initialization Complete\");\n            \/\/ Authenticate your credentials\n            m_api = api;\n            m_api.TTAPIStatusUpdate += new EventHandler&lt;TTAPIStatusUpdateEventArgs&gt;(m_api_TTAPIStatusUpdate);\n            m_api.Start();\n        }\n        else if (ex.IsRecoverable)\n        {\n            \/\/ this is in informational update from the SDK\n            Console.WriteLine(\"TT.NET SDK Initialization Message: {0}\", ex.Message);\n            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionAvailable)\n            {\n                \/\/ a newer version of the SDK is available - notify someone to upgrade\n            }\n        }\n        else\n        {\n            Console.WriteLine(\"TT.NET SDK Initialization Failed: {0}\", ex.Message);\n            if (ex.Code == ApiCreationException.ApiCreationError.NewAPIVersionRequired)\n            {\n                \/\/ do something to upgrade the SDK package since it will not start until it is upgraded \n                \/\/ to the minimum version noted in the exception message\n            }\n            Dispose();\n        }\n    }\n\n    public void m_api_TTAPIStatusUpdate(object sender, TTAPIStatusUpdateEventArgs e)\n    {\n        if (e.IsReady)\n        {\n            \/\/ connection to TT is established\n            Console.WriteLine(\"TT.NET SDK Authenticated\");\n        }\n        else if (ex.IsDown)\n        {\n            \/\/ connection is down but TT .NET SDK will automatically attempt to reconnect\n            Console.WriteLine(\"Connection down. Reconnecting...\");\n        }\n        else\n        {\n            Console.WriteLine(\"TT.NET SDK Status: {0}\", e);\n        }\n    }\n\n    public void Dispose()\n    {\n        TTAPI.ShutdownCompleted += TTAPI_ShutdownCompleted;\n        TTAPI.Shutdown();\n    }\n\n    public void TTAPI_ShutdownCompleted(object sender, EventArgs e)\n    {\n        Console.WriteLine(\"TTAPI Shutdown completed\");\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To create a Console application, you need to: Client-side mode example The following code snippet demonstrates [&hellip;]<\/p>\n","protected":false},"author":2,"template":"wp-custom-template-single-doc-tt-net-sdk","meta":{"_acf_changed":true,"footnotes":""},"docs-category":[770],"class_list":["post-8041","doc","type-doc","status-publish","hentry","docs-category-creating-the-application-framework"],"acf":[],"_links":{"self":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc\/8041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc"}],"about":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/types\/doc"}],"author":[{"embeddable":true,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":0,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc\/8041\/revisions"}],"wp:attachment":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/media?parent=8041"}],"wp:term":[{"taxonomy":"docs-category","embeddable":true,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/docs-category?post=8041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}