{"id":1691,"date":"2019-12-12T15:50:04","date_gmt":"2019-12-12T22:50:04","guid":{"rendered":"http:\/\/cknotes.com\/?p=1691"},"modified":"2019-12-12T15:50:04","modified_gmt":"2019-12-12T22:50:04","slug":"net-core-async-method-call-with-taskcompleted-callback","status":"publish","type":"post","link":"https:\/\/cknotes.com\/net-core-async-method-call-with-taskcompleted-callback\/","title":{"rendered":".NET Core Async Method Call with TaskCompleted Callback"},"content":{"rendered":"Here is a complete example showing how to write a call to an asynchronous Chilkat method in .NET Core.  (Note: This is .NET Core, not .NET Framework.)\r\nThis example uses the TaskCompleted callback to get notified when the task completes.\r\n\r\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections;\r\n\r\nnamespace ChilkatTest\r\n{\r\n    class Program\r\n    {\r\n        bool m_taskFinished = false;\r\n\r\n        Hashtable m_chilkatTasks = new Hashtable();\r\n        Hashtable m_chilkatSockets = new Hashtable();\r\n\r\n        \/\/ This callback will occur within the background thread..\r\n        public void Socket_HandleTaskIdCompleted(int taskId)\r\n        {\r\n            \/\/ Get the Chilkat task for this callback.\r\n            Chilkat.Task task = (Chilkat.Task) m_chilkatTasks&#x5B;taskId];\r\n            if (task == null)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(&quot;Task not found.&quot;);\r\n                return;\r\n            }\r\n\r\n            \/\/ Get the Chilkat socket.\r\n            Chilkat.Socket socket = (Chilkat.Socket)m_chilkatSockets&#x5B;taskId];\r\n            if (socket == null)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(&quot;Socket not found.&quot;);\r\n                return;\r\n            }\r\n\r\n            \/\/ Do whatever we want..\r\n\r\n            \/\/ For this example, examine the contents of the socket.LastErrorText for both success and failure.\r\n            System.Diagnostics.Debug.WriteLine(socket.LastErrorText);\r\n\r\n            \/\/ Did the call socket connect successfully?\r\n            if (task.GetResultBool() == true)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(&quot;The TLS connection was successful.&quot;);\r\n            }\r\n            else\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(&quot;The TLS connection failed.&quot;);\r\n            }\r\n\r\n            m_chilkatTasks.Remove(taskId);\r\n            m_chilkatSockets.Remove(taskId);\r\n\r\n            m_taskFinished = true;\r\n        }\r\n\r\n\r\n        \/\/ .NET Core async with TaskFinished callback.\r\n        void TestSocketConnectAsync()\r\n        {\r\n            Chilkat.Socket socket = new Chilkat.Socket();\r\n\r\n            \/\/ Setup the TaskCompleted callback.\r\n            Chilkat.Socket.TaskIdCompleted taskIdCompleted = new Chilkat.Socket.TaskIdCompleted(Socket_HandleTaskIdCompleted);\r\n            socket.setTaskIdCompletedCb(taskIdCompleted);\r\n\r\n            Chilkat.Task task = socket.ConnectAsync(&quot;google.com&quot;, 443, true, 5000);\r\n            if (socket.LastMethodSuccess == false)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(socket.LastErrorText);\r\n                return;\r\n            }\r\n\r\n            \/\/ Save the task to our hashtable so we can access the Chilkat.Task object from within the TaskCompleted callback.\r\n            m_chilkatTasks&#x5B;task.TaskId] = task;\r\n\r\n            \/\/ Also save the socket object for access within the TaskCompleted callback.\r\n            m_chilkatSockets&#x5B;task.TaskId] = socket;\r\n\r\n\r\n            \/\/ Schedule the task for running on the thread pool.  This changes the task&#039;s state\r\n            \/\/ from Inert to Live.\r\n            bool success = task.Run();\r\n            if (success != true)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(task.LastErrorText);\r\n                return;\r\n            }\r\n\r\n        }\r\n\r\n        void WaitUntilFinished()\r\n        {\r\n            while (!m_taskFinished)\r\n            {\r\n                System.Threading.Thread.Sleep(100);\r\n            }\r\n        }\r\n\r\n        static bool UnlockChilkat()\r\n        {\r\n            Chilkat.Global glob = new Chilkat.Global();\r\n            bool unlocked = glob.UnlockBundle(&quot;trial&quot;);\r\n            if (!unlocked)\r\n            {\r\n                System.Diagnostics.Debug.WriteLine(glob.LastErrorText);\r\n            }\r\n            return unlocked;\r\n        }\r\n\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            if (!UnlockChilkat()) return;\r\n\r\n            Program prog = new Program();\r\n            prog.TestSocketConnectAsync();\r\n\r\n            \/\/ Wait for the async task to finish before exiting the program.\r\n            prog.WaitUntilFinished();\r\n\r\n            System.Diagnostics.Debug.WriteLine(&quot;Exiting..&quot;);\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre><\/div>\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Here is a complete example showing how to write a call to an asynchronous Chilkat method in .NET Core. (Note: This is .NET Core, not .NET Framework.) This example uses the TaskCompleted callback to get notified when the task completes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[738,425,494,222],"class_list":["post-1691","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-net-core","tag-async","tag-asynchronous","tag-callbacks"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1691"}],"version-history":[{"count":1,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1691\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}