{"id":22,"date":"2008-01-06T22:39:12","date_gmt":"2008-01-07T02:39:12","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=22"},"modified":"2008-01-07T00:34:30","modified_gmt":"2008-01-07T04:34:30","slug":"asynchronous-operations-that-use-iasyncresult-objects","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=22","title":{"rendered":"Asynchronous operations that use IAsyncResult objects"},"content":{"rendered":"<p>Implemented as two methods<\/p>\n<ul>\n<li><em>BeginOperation<\/em>\n<ul>\n<li>Begines\u00a0operation asynchronously in a background thread from thread pool<\/li>\n<li>Returns an object of type <em>IAsyncResult<\/em><\/li>\n<\/ul>\n<\/li>\n<li><em>EndOperation<\/em>\n<ul>\n<li>Get results from the operation<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Accessing results of an\u00a0async operation<\/p>\n<ul>\n<li>Need to wait for results from async operation\n<ul>\n<li>Call <em>EndOperation<\/em> from applications&#8217;s main method to block main application execution\n<pre lang=\"csharp\">\r\n\/\/ Start the asynchronous request for DNS information.\u00a0 \r\n\/\/ This example does not use a delegate or user-supplied object \r\n\/\/ so the last two arguments are null. \r\nIAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); \r\nConsole.WriteLine(\"Processing your request for information...\");\u00a0 \r\n\/\/ Do any additional work that can be done here. \r\ntry { \r\n    \/\/ EndGetHostByName blocks until the process completes. \r\n    IPHostEntry host = Dns.EndGetHostEntry(result);<\/pre>\n<\/li>\n<li>Use <em>AsyncWaitHandle<\/em> to block main application execution\n<pre lang=\"csharp\">\r\n\/\/ Start the asynchronous request for DNS information.\u00a0 \r\nIAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);\u00a0 \r\nConsole.WriteLine(\"Processing request for information...\");\u00a0 \r\n\/\/ Wait until the operation completes.\u00a0 \r\nresult.AsyncWaitHandle.WaitOne(); \r\n\/\/ The operation completed. Process the results. \r\ntry \r\n{\r\n  \/\/ Get the results.\r\n  IPHostEntry host = Dns.EndGetHostEntry(result);\r\n\u00a0<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>No need to wait for async operation\n<ul>\n<li>Check <em>IsComplete<\/em> property periodically and call <em>EndOperation<\/em> when completed\n<pre lang=\"csharp\">\r\n\/\/ Start the asychronous request for DNS information. \r\nIAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); \r\nConsole.WriteLine(\"Processing request for information...\");        \r\n\r\n\/\/ Poll for completion information. \r\n\/\/ Print periods (\".\") until the operation completes. \r\nwhile (result.IsCompleted != true) \r\n{ \r\n    UpdateUserInterface(); \r\n} \r\n\/\/ The operation is complete. Process the results. \r\n\/\/ Print a new line. \r\nConsole.WriteLine(); \r\ntry \r\n{ \r\n    IPHostEntry host = Dns.EndGetHostEntry(result);<\/pre>\n<\/li>\n<li>Use an <em>AsyncCallback<\/em> delegate to specify a method to be invoked when async operation is completed\n<pre lang=\"csharp\">\r\npublic static void Main() \r\n{ \r\n    \/\/ Create the delegate that will process the results of the \r\n    \/\/ asynchronous request. \r\n    AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation); \r\n    string host; \r\n    do \r\n    { \r\n        Console.Write( \r\n            \" Enter host computer or <enter><\/enter> to finish: \"); \r\n        host = Console.ReadLine(); \r\n        if (host.Length &gt; 0) \r\n        { \r\n            \/\/ Increment the request counter in a thread safe manner. \r\n            Interlocked.Increment(ref requestCounter); \r\n            \/\/ Start the asynchronous request for DNS information. \r\n            Dns.BeginGetHostEntry(host, callBack, host); \r\n         } \r\n    } while (host.Length &gt; 0); \r\n}     \r\n\r\n\/\/ The following method is called \r\n\/\/when each asynchronous operation completes. \r\nstatic void ProcessDnsInformation(IAsyncResult result) \r\n{ \r\n    string hostName = (string) result.AsyncState; \r\n    hostNames.Add(hostName); \r\n    try \r\n    { \r\n        \/\/ Get the results. \r\n        IPHostEntry host = Dns.EndGetHostEntry(result); \r\n        hostData.Add(host); \r\n    } \r\n    \/\/ Store the exception message. \r\n    catch (SocketException e) \r\n    { \r\n        hostData.Add(e.Message); \r\n    } \r\n    finally \r\n    { \r\n        \/\/ Decrement the request counter in a thread-safe manner. \r\n        Interlocked.Decrement(ref requestCounter); \r\n    } \r\n}<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>Implemented as two methods BeginOperation Begines\u00a0operation asynchronously in a background thread from thread pool Returns an object of type IAsyncResult EndOperation Get results from the operation Accessing results of an\u00a0async operation Need to wait for results from async operation Call &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=22\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-m","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}