{"id":1320,"date":"2023-10-01T13:09:15","date_gmt":"2023-10-01T05:09:15","guid":{"rendered":"https:\/\/summid.icu\/?p=1320"},"modified":"2023-10-01T13:09:40","modified_gmt":"2023-10-01T05:09:40","slug":"stasktutorials2","status":"publish","type":"post","link":"https:\/\/summid.icu\/index.php\/2023\/10\/01\/stasktutorials2\/","title":{"rendered":"Unity\u5f02\u6b65\u6269\u5c55\u5b9e\u8df5\uff08\u4e8c\uff09\u2014\u2014\u4ee5UniTask\u5e93\u4e3a\u53c2\u8003"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u603b\u89c8<\/h2>\n\n\n\n<p>\u6b63\u5f0f\u7f16\u5199\u4ee3\u7801\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5f02\u6b65\u6846\u67b6\u6709\u4e00\u4e2a\u5927\u4f53\u7684\u8ba4\u8bc6\uff0c\u56e0\u6b64\u6211\u7b80\u5355\u753b\u4e86\u4e00\u5e45\u7c7b\u56fe\uff0c\u5e2e\u52a9\u5927\u5bb6\u7559\u4e2a\u5370\u8c61\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskMain-1024x871.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"871\" data-original=\"https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskMain-1024x871.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1309\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u9664\u4e86\u63a5\u53e3\u548c\u7c7b <code>AsyncSTask<\/code> \u3001<code>TaskPool<\/code> \u4ee5\u5916\uff0c\u5927\u90e8\u5206\u7ed3\u6784\u90fd\u662f\u7ed3\u6784\u4f53struct\u7c7b\u578b\uff0c\u540c\u65f6\u6211\u4e5f\u5728\u56fe\u4e2d\u7279\u522b\u6ce8\u660e\u4e86\uff0c<code>TaskPool<\/code> \u4e3a\u9759\u6001\u7c7b\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u56fe\u6211\u4eec\u5f97\u77e5\uff0c\u72b6\u6001\u673a\u63a5\u53e3 <code>AsyncSTaskMethodBuilder<\/code> \u901a\u8fc7\u63a5\u53e3 <code>IStateMachineRunnerPromise<\/code> \u6765\u4e0e\u5904\u7406\u5f02\u6b65\u903b\u8f91\u7684\u6253\u5de5\u4ed4 <code>AsyncSTask<\/code> \u5efa\u7acb\u8fde\u63a5\uff0c\u800c\u771f\u6b63\u7684\u5f02\u6b65\u6838\u5fc3\u673a\u5236\u5219\u4ea4\u7ed9 <code>STaskCompletionSourceCore<\/code> \u6765\u5b8c\u6210\u3002\u7531\u4e8e <code>AsyncSTask<\/code> \u5b9e\u73b0\u7684\u63a5\u53e3\u6bd4\u8f83\u591a\uff0c\u4e3a\u4e86\u66f4\u6e05\u695a\u5730\u6d4f\u89c8\uff0c\u6211\u5c06\u5b83\u7684\u6210\u5458\u8fdb\u884c\u5206\u7c7b\u5c55\u793a\u3002\u72b6\u6001\u673a\u6bcf\u6b21\u7b49\u5f85\u672a\u5b8c\u6210\u7684 <code>STask<\/code> \u65f6\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a <code>AsyncSTask<\/code> \u5bf9\u8c61\uff08\u521b\u5efa\u8fc7\u7a0b\u540e\u7eed\u4f1a\u8ba8\u8bba\uff09\uff0c\u56e0\u6b64\u4e3a\u4e86\u6027\u80fd\u8003\u8651\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b83\u201c\u6c60\u5316\u201d\u2014\u2014\u5f15\u5165\u5bf9\u8c61\u6c60 <code>TaskPool<\/code> \u3002<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u7b2c\u4e00\u7ae0\u7684\u4ecb\u7ecd\uff0c\u5927\u4f19\u513f\u5e94\u8be5\u4e0d\u4f1a\u5bf9\u53e6\u4e00\u8fb9\u7684 <code>STask<\/code> \u548c <code>Awaiter<\/code> \u592a\u964c\u751f\u4e86\u3002<code>STask<\/code> \u9700\u8981\u5b9e\u73b0 <code>GetAwaiter()<\/code> \u65b9\u6cd5\uff0c\u56e0\u6b64\u4ed6\u4fe9\u5929\u751f\u5c31\u662f\u4e00\u5bf9\u3002\u800c <code>STask<\/code> \u548c <code>AsyncSTask<\/code> \u7684\u5173\u7cfb\u5219\u662f\u9760\u63a5\u53e3 <code>ISTaskSource<\/code> \u5efa\u7acb\u8d77\u6765\u7684\u3002<\/p>\n\n\n\n<p>\u540e\u9762\u6211\u4eec\u9996\u5148\u4f1a\u8ba8\u8bba\u5bf9\u8c61\u6c60 <code>TaskPool<\/code> \u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff0c\u56e0\u4e3a\u5728\u6574\u4e2a\u6846\u67b6\u4e2d\uff0c\u5b83\u76f8\u5bf9\u72ec\u7acb\uff0c\u539f\u7406\u4e5f\u7b80\u5355\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5176\u4ed6\u6a21\u5757\u7684\u7ec6\u8282\u3002\u6211\u4f1a\u4f18\u5148\u4ecb\u7ecd\u6bd4\u8f83\u91cd\u8981\u7684\u90e8\u5206\uff0c\u5982 <code>STaskCompletionSourceCore<\/code> \u3002\u8fd9\u4e0d\u4e00\u5b9a\u662f\u6700\u63a8\u8350\u7684\u9605\u8bfb\u987a\u5e8f\uff0c\u6bd4\u5982\u6709\u7684\u5c0f\u4f19\u4f34\u66f4\u4e60\u60ef\u81ea\u4e0a\u800c\u4e0b\u5730\u62c6\u5206\u7cfb\u7edf\u8fdb\u884c\u5b66\u4e60\uff0c\u6bd4\u5982\u4ece <code>AsyncSTaskMethodBuilder<\/code> \u5f00\u59cb\uff0c\u56e0\u6b64\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u5c0f\u8282\u5f00\u59cb\u3002<\/p>\n\n\n\n<p>\u6ce8\uff1a\u51fa\u4e8e\u7bc7\u5e45\u8003\u8651\uff0c\u6211\u4eec\u53ea\u4f1a\u5206\u6790\u6709\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u65b9\u6cd5\uff0c\u5728\u5b9e\u73b0\u4e0a\u5b83\u6bd4\u65e0\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u65b9\u6cd5\u591a\u4e86\u8fd4\u56de\u7c7b\u578b\u6cdb\u578b\uff0c\u5e76\u4e14\u591a\u4e86\u8bbe\u7f6e\u3001\u83b7\u53d6\u65b9\u6cd5\u7ed3\u679c\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TaskPool<\/h2>\n\n\n\n<p><code>TaskPool<\/code> \u5185\u90e8\u53ef\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u5176\u4e2d<strong>\u9759\u6001\u7c7b<\/strong> <code>TaskPool<\/code> \u662f\u7eaf\u7eaf\u7684\u5de5\u5177\u7c7b\uff0c\u63d0\u4f9b\u83b7\u53d6\u5bf9\u8c61\u6c60\u5bb9\u91cf\u7684\u63a5\u53e3\uff0c<strong>\u4e0d\u5b9e\u73b0<\/strong>\u5bf9\u8c61\u6c60\u529f\u80fd\uff1b\u53e6\u4e00\u90e8\u5206 <code>Task&lt;T&gt;<\/code> \u5219\u662f\u5bf9\u8c61\u6c60\u4e3b\u4f53\uff0c\u63d0\u4f9b\u5b58\u53d6\u63a5\u53e3\uff0c\u51fa\u4e8e\u6027\u80fd\u8003\u8651\uff0c\u6211\u4eec\u5c06\u5b83\u5b9a\u4e49\u4e3a\u7ed3\u6784\u4f53\u7c7b\u578b\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u63a5\u53e3 <code>ITaskPoolNode&lt;T&gt;<\/code> \uff0c\u4efb\u4f55\u8981\u4f7f\u7528\u5bf9\u8c61\u6c60\u7684\u5bf9\u8c61\u9700\u8981\u5b9e\u73b0\u5b83\u7684\u5c5e\u6027 <code>NextNode<\/code> \u3002\u8fd9\u4e00\u8282\u7684\u4ee3\u7801\u53ef\u4ee5\u5728 TaskPool.cs \u6587\u4ef6\u4e2d\u627e\u5230\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">static class TaskPool<\/h3>\n\n\n\n<p>\u5de5\u5177\u7c7b <code>TaskPool<\/code> \u76f8\u5bf9\u6bd4\u8f83\u72ec\u7acb\uff0c\u6211\u4eec\u5148\u6765\u5206\u6790\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/\/ &lt;summary>\r\n    \/\/\/ TaskPool\u5de5\u5177\u7c7b\uff0c\u63d0\u4f9b\u83b7\u53d6\u5bf9\u8c61\u6c60\u5bb9\u91cf\u7684\u63a5\u53e3\uff0c\u4e0d\u5b9e\u73b0\u5bf9\u8c61\u6c60\u529f\u80fd\r\n    \/\/\/ &lt;\/summary>\r\n    public static class TaskPool\r\n    {\r\n        internal static int MaxPoolSize;\r\n\r\n        private static Dictionary&lt;Type, Func&lt;int>> sizes = new Dictionary&lt;Type, Func&lt;int>>();\r\n\r\n        static TaskPool()\r\n        {\r\n            \/\/\u5148\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u5bfb\u627e\u9884\u5b9a\u4e49\u7684\u5927\u5c0f\uff0c\u82e5\u6ca1\u627e\u5230\u5219\u9ed8\u8ba4\u6700\u5927\u503c\r\n            try\r\n            {\r\n                string value = Environment.GetEnvironmentVariable(\"STASK_MAX_POOLSIZE\");\r\n                if (value != null)\r\n                {\r\n                    if (int.TryParse(value, out int size))\r\n                    {\r\n                        MaxPoolSize = size;\r\n                        return;\r\n                    }\r\n                }\r\n            }\r\n            catch { }\r\n\r\n            MaxPoolSize = int.MaxValue;\r\n        }\r\n\r\n        public static void SetMaxPoolSize(int maxPoolSize)\r\n        {\r\n            MaxPoolSize = maxPoolSize;\r\n        }\r\n\r\n        public static IEnumerable&lt;(Type, int)> GetCacheSizeInfo()\r\n        {\r\n            lock (sizes)\r\n            {\r\n                foreach (KeyValuePair&lt;Type, Func&lt;int>> item in sizes)\r\n                {\r\n                    yield return (item.Key, item.Value());\r\n                } \r\n            }\r\n        }\r\n\r\n        public static void RegisterSizeGetter(Type type, Func&lt;int> getSize)\r\n        {\r\n            lock (sizes)\r\n            {\r\n                sizes&#91;type] = getSize;\r\n            }\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53 <code>TaskPool<\/code> \u7c7b\u7b2c\u4e00\u6b21\u88ab\u5176\u4ed6\u4eba\u8c03\u7528\uff0c\u4f1a\u5148\u8fdb\u5165\u9759\u6001\u6784\u9020\u65b9\u6cd5\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u8bbe\u7f6e\u6c60\u5b50\u7684\u6700\u5927\u5bb9\u91cf\u3002<\/li>\n\n\n\n<li>\u6c60\u5b50\u4f1a\u4f18\u5148\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u4e2d\u9884\u5b9a\u4e49\u7684\u5bb9\u91cf\uff08\u5982\u679c\u6709\u5b9a\u4e49\uff09\uff0c\u82e5\u6ca1\u6709\u5219\u53d6\u6574\u578b\u7684\u6700\u5927\u503c\u3002\u9664\u4e86\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6e\u5bb9\u91cf\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7 <code>SetMaxPoolSize(int)<\/code> \u6765\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li>\u6211\u4eec\u53ef\u901a\u8fc7 <code>TaskPool<\/code> \u7c7b\u6765\u83b7\u53d6\u5177\u4f53\u67d0\u4e2a\u6c60\u5b50\u5f53\u524d\u7684\u5927\u5c0f\uff0c\u4f46\u83b7\u53d6\u7684\u65b9\u6cd5\u9700\u8981\u5916\u90e8\u63d0\u4f9b\uff08\u4e00\u822c\u6765\u8bf4\u5c31\u7531\u4f7f\u7528\u6c60\u5b50\u7684\u5bf9\u8c61\u63d0\u4f9b\uff09\u3002\u4f7f\u7528\u5bf9\u8c61\u6c60\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u987a\u4fbf\u8c03\u7528 <code>RegisterSizeGetter(Type, Func&lt;int&gt;)<\/code> \u6765\u6ce8\u518c\u83b7\u53d6\u5bf9\u8c61\u6c60\u5927\u5c0f\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5f53\u6211\u4eec\u9700\u8981\u67e5\u8be2\u5f53\u524d\u7684\u5bf9\u8c61\u6c60\u4eec\u7684\u5927\u5c0f\u65f6\uff0c\u53ef\u901a\u8fc7 <code>GetCacheSizeInfo()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u8be5\u65b9\u6cd5\u5185\u90e8\u7531 yield return \u5b9e\u73b0\uff0c\u82e5\u7528\u6237\u9700\u8981\u67e5\u8be2\u67d0\u4e00\u4e2a\u6c60\u5b50\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528 foreach \u6765\u8fed\u4ee3\u5176\u8fd4\u56de\u7684 <code>IEnumerable&lt;Type, int&gt;<\/code> \u5bf9\u8c61\uff0c\u5e76\u5728\u627e\u5230\u76ee\u6807\u6c60\u5b50\u540e\u8df3\u51fa\u5faa\u73af\uff0c\u4e0d\u7528\u62c5\u5fc3\u51fa\u73b0\u8fc7\u591a\u7684\u8fed\u4ee3\u4e0e\u8c03\u7528\u3002\u8fd9\u6837\u5199\u7684\u597d\u5904\u662f\u65b9\u6cd5\u53ea\u5b9e\u73b0\u6700\u57fa\u672c\u7684\u529f\u80fd\uff0c\u8ba9\u5de5\u5177\u7c7b\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u81f3\u4e8e\u662f\u67e5\u8be2\u6240\u6709\u5bf9\u8c61\u6c60\u8fd8\u662f\u67d0\u4e00\u4e2a\u5bf9\u8c61\u6c60\uff0c\u5219\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">interface ITaskPoolNode&lt;T&gt;<\/h3>\n\n\n\n<p>\u5047\u8bbe\u5728\u4e00\u6b3e\u5c04\u51fb\u6e38\u620f\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5b50\u5f39\u7c7b <code>Bullet<\/code> \uff0c\u5728\u5408\u9002\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5c06 <code>Bullet<\/code> \u5bf9\u8c61\u653e\u8fdb\u5bf9\u8c61\u6c60\u3002\u5728 <code>TaskPool<\/code> \u8fd9\u4e00\u5957\u5bf9\u8c61\u6c60\u5de5\u5177\u4e0b\uff0c<code>Bullet<\/code> \u9700\u8981\u5b9e\u73b0 <code>ITaskPoolNode&lt;T><\/code> \u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface ITaskPoolNode&lt;T>\r\n{\r\n    ref T NextNode { get; }\r\n}\r\n\r\npublic class Bullet : ITaskPoolNode&lt;Bullet>\r\n{\r\n\tpublic ref Bullet NextNode { get; }\r\n}<\/code><\/pre>\n\n\n\n<p>\u770b\u5230 <code>NextNode<\/code> \u5c5e\u6027\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u6bd4\u8f83\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u8054\u60f3\u5230\u94fe\u8868\u3002\u6ca1\u9519\uff0c\u7531\u4e8e\u5bf9\u8c61\u6c60\u5bb9\u5668\u9700\u8981\u9891\u7e41\u5730\u5b58\u53d6\u5bf9\u8c61\uff0c\u6211\u4eec\u5728\u6b64\u4f7f\u7528\u94fe\u8868\u5c06\u5176\u4f18\u52bf\u53d1\u626c\u5149\u5927\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\uff0c\u6211\u4eec\u4e0d\u4f1a\u8ba9\u8282\u70b9\u5bf9\u8c61\u81ea\u5df1\u6301\u6709 <code>NextNode<\/code> \u5c5e\u6027\uff0c\u56e0\u4e3a\u5728C#\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u53cc\u5411\u94fe\u8868 <code>LinkedList&lt;T&gt;<\/code> \u3002\u8fd9\u6837\u505a\u5341\u5206\u65b9\u4fbf\uff0c\u4f46\u4ee3\u4ef7\u662f\u6211\u4eec\u9700\u8981\u7ed9\u6bcf\u4e00\u79cd\u8282\u70b9\u90fd\u5f00\u4e00\u4e2a <code>LinkedList&lt;T&gt;<\/code> \u5bb9\u5668\u3002\u800c\u5728\u6e38\u620f\u8fd0\u884c\u65f6\uff0c\u53ef\u80fd\u6709\u51e0\u5341\u4e0a\u767e\u4e2a task \u5e76\u53d1\u8fd0\u884c\uff0c\u867d\u8bf4\u90fd\u662f task \uff0c\u4f46\u8003\u8651\u5230\u6cdb\u578b\uff0c\u6211\u4eec\u4ecd\u53ef\u80fd\u8981\u7ef4\u62a4\u5f88\u591a\u4e2a <code>LinkedList&lt;T&gt;<\/code> \uff0c\u8fd9\u662f\u6211\u4eec\u60f3\u907f\u514d\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u628a\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u529f\u80fd\u653e\u8fdb\u8282\u70b9\u81ea\u8eab\u3002<\/p>\n\n\n\n<p>\u8282\u70b9\u7684\u6570\u636e\u7c7b\u578b\u6709\u53ef\u80fd\u662f\u201c\u503c\u7c7b\u578b\u201d\u7684\uff0c\u6bd4\u5982 struct \uff0c\u6240\u4ee5 <code>NextNode<\/code> \u5c5e\u6027\u52a0\u4e86 ref \u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u8ba8\u8bba\u5bf9\u8c61\u6c60\u6700\u6838\u5fc3\u903b\u8f91\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">struct TaskPool&lt;T&gt;<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    &#91;StructLayout(LayoutKind.Auto)]\r\n    public struct TaskPool&lt;T> where T : class, ITaskPoolNode&lt;T>\r\n    {\r\n        private int gate;\r\n        private int size;\r\n        private T root;\/\/root\u4e3a\u94fe\u8868\u5934\uff0c\u51fa\u961f\u4e0e\u5165\u961f\u90fd\u5728root\u4f4d\u7f6e\u64cd\u4f5c\uff1bnode0 &lt;-- node1 &lt;-- node2 &lt;-- node3 (root), node3's NextNode is node2\r\n\r\n        public int Size => this.size;\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public bool TryPop(out T result)\r\n        {\r\n            \/\/if gate equals 'comparand', it will be replaced by 'value', return original value in 'location1'\r\n            if (Interlocked.CompareExchange(ref this.gate, 1, 0) == 0)\r\n            {\r\n                T v = this.root;\r\n                if (!(v is null))\r\n                {\r\n                    ref T nextNode = ref v.NextNode;\r\n                    this.root = nextNode;\r\n                    nextNode = null;\r\n                    this.size--;\r\n                    result = v;\r\n                    \/\/The 'Volatile.Write' method forces the value in 'location' to be written to at the point of the call.\r\n                    \/\/In addition, any earlier program-order loads and stores must occur before the call to Volatile.Write.\r\n                    Volatile.Write(ref this.gate, 0);\r\n                    return true;\r\n                }\r\n\r\n                Volatile.Write(ref this.gate, 0);\r\n            }\r\n\r\n            result = default;\r\n            return false;\r\n        }\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public bool TryPush(T item)\r\n        {\r\n            if (Interlocked.CompareExchange(ref this.gate, 1, 0) == 0)\r\n            {\r\n                if (this.size &lt; TaskPool.MaxPoolSize)\r\n                {\r\n                    item.NextNode = this.root;\r\n                    this.root = item;\r\n                    this.size++;\r\n                    Volatile.Write(ref this.gate, 0);\r\n                    return true;\r\n                }\r\n\r\n                Volatile.Write(ref this.gate, 0);\r\n            }\r\n\r\n            return false;\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u91cd\u70b9\u5728 <code>TryPop(out T)<\/code> \u548c <code>TryPush(T)<\/code> \u65b9\u6cd5\u4e0a\uff0c\u903b\u8f91\u90fd\u6bd4\u8f83\u7b80\u5355\uff0c\u4e0b\u9762\u6311\u51e0\u4e2a\u7279\u522b\u7684\u5730\u65b9\u8bf4\u660e\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u94fe\u8868\u7684\u5934\u6307\u9488 <code>root<\/code> \uff08\u6839\u636e\u5b83\u7684\u547d\u540d\uff0c\u8fd9\u91cc\u7b14\u8005\u5c06\u5176\u79f0\u4e3a\u5934\u6307\u9488\uff09\uff0c\u662f\u5b58\u653e\u548c\u53d6\u51fa\u8282\u70b9\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u6709\u5c0f\u4f19\u4f34\u624b\u6413\u8fc7\u94fe\u8868\uff0c\u5e94\u8be5\u542c\u8bf4\u8fc7\u94fe\u8868\u7684\u201c\u5934\u63d2\u6cd5\u201d\u548c\u201c\u5c3e\u63d2\u6cd5\u201d\uff0c\u8fd9\u91cc\u91c7\u7528\u7684\u524d\u8005\u3002<\/li>\n\n\n\n<li>\u8003\u8651\u5230\u591a\u7ebf\u7a0b\u5b89\u5168\uff0c\u5728\u5b58\u53d6\u64cd\u4f5c\u4e4b\u524d\u90fd\u4f1a\u52a0\u9501\uff0c\u800c\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u5f02\u6b65\u5e93\u672c\u8eab\u5c31\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u56e0\u6b64\u5728\u9047\u5230\u4e0a\u4e86\u9501\u7684\u60c5\u51b5\u4f1a\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n\n\n\n<li>\u5224\u65ad\u6709\u6ca1\u6709\u4e0a\u9501\uff0c\u548c\u4e0a\u9501\u7684\u64cd\u4f5c\u662f\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c <code>Interlocked.CompareExchange(int, int, int)<\/code> \u6765\u5b9e\u73b0\u7684\u3002\u5b83\u4f1a\u5224\u65ad <code>gate<\/code> \u662f\u5426\u7b49\u4e8e\u7b2c\u4e09\u4e2a\u53c2\u6570 <code>0<\/code> \uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5219\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u503c <code>1<\/code> \u8d4b\u7ed9 <code>gate<\/code> \uff0c\u6700\u7ec8\u8fd4\u56de <code>gate<\/code> \u539f\u672c\u7684\u503c\u3002<del>\u4e00\u884c\u4ee3\u7801\u5c31\u5b8c\u6210\u4e86\u8fd9\u4e24\u4ef6\u4e8b\uff0c\u592a\u4f18\u96c5\u4e86\uff01<\/del><\/li>\n\n\n\n<li>\u6700\u540e\u89e3\u9501\u64cd\u4f5c\u4f7f\u7528\u7684 <code>Volatile.Write(int, int)<\/code> \uff0c\u8868\u793a\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5199\u8fdb\u7b2c\u4e00\u4e2a\u53c2\u6570\u91cc\u3002\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u8d4b\u503c\uff0c\u4e5f\u662f\u8003\u8651\u5230\u591a\u7ebf\u7a0b\u7684\u95ee\u9898\u2014\u2014 <code>gate<\/code> \u53ef\u80fd\u7531\u591a\u4e2a\u540c\u65f6\u6267\u884c\u7684\u7ebf\u7a0b\u4fee\u6539\uff0c\u51fa\u4e8e\u6027\u80fd\u8003\u8651\uff0c\u7f16\u8bd1\u5668\uff0c\u8fd0\u884c\u65f6\u7cfb\u7edf\u751a\u81f3\u786c\u4ef6\u90fd\u53ef\u80fd\u91cd\u65b0\u6392\u5217\u5bf9\u5b58\u50a8\u5668\u4f4d\u7f6e\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002\u610f\u601d\u5c31\u662f\u5982\u679c\u76f4\u63a5\u5199\u5165\uff0c\u90a3\u4e48\u8fd9\u4e00\u884c\u4ee3\u7801\u53ef\u80fd\u4f1a\u88ab\u4f18\u5316\u5230\u5176\u4ed6\u5730\u65b9\uff0c\u5bfc\u81f4\u4ee3\u7801\u7684\u6267\u884c\u987a\u5e8f\u88ab\u4fee\u6539\u3002\u4f7f\u7528 <code>Volatile.Write(int, int)<\/code> \u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u6267\u884c\u987a\u5e8f\u4e0d\u4f1a\u6539\u53d8\uff0c\u5c06\u5b83\u201c\u56fa\u5b9a\u4f4f\u201d\u3002<\/li>\n\n\n\n<li><code>TaskPool&lt;T><\/code> \u88ab\u5b9a\u4e49\u4e3a struct \u7c7b\u578b\uff0c\u4e5f\u662f\u4e3a\u6027\u80fd\u505a\u7684\u8ba9\u6b65\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u6700\u597d\u4e0d\u8981\u62f7\u8d1d\u5b83\u3002\u5176\u5b9e\u5728\u540e\u7eed\u7684\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u5c06\u5b83\u5b9a\u4e49\u6210\u9759\u6001\u53d8\u91cf\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    public class TestTaskPoolNode : ITaskPoolNode&lt;TestTaskPoolNode>\r\n    {\r\n        private static TaskPool&lt;TestTaskPoolNode> pool;\r\n\r\n        private TestTaskPoolNode nextNode;\r\n        public ref TestTaskPoolNode NextNode => ref this.nextNode;\r\n\r\n        static TestTaskPoolNode()\r\n        {\r\n            TaskPool.RegisterSizeGetter(typeof(TestTaskPoolNode), () => pool.Size);\r\n        }\r\n\r\n        public static TestTaskPoolNode Create()\r\n        {\r\n            if (pool.TryPop(out var node))\r\n            {\r\n                node = new TestTaskPoolNode();\r\n            }\r\n            return node;\r\n        }\r\n        \r\n        public bool Dispose()\r\n        {\r\n            return pool.TryPush(this);\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4f1a\u76f4\u63a5\u628a\u5bf9\u8c61\u6c60\u653e\u5728\u8282\u70b9\u91cc\uff0c\u901a\u8fc7\u9759\u6001\u65b9\u6cd5 <code>Create()<\/code> \u53bb\u83b7\u53d6\u65b0\u8282\u70b9\uff0c\u4f7f\u7528\u5b8c\u540e\u8c03\u7528 <code>Dispose()<\/code> \u653e\u8fdb\u5bf9\u8c61\u6c60\uff0c\u7528\u6237\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u751a\u81f3\u611f\u53d7\u4e0d\u5230\u5bf9\u8c61\u6c60\u7684\u5b58\u5728\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<p>\u8fd9\u4e00\u8282\u4ecb\u7ecd\u4e86 <code>STask<\/code> \u4e2d\u7684\u5bf9\u8c61\u6c60\uff0c\u5b83\u662f\u5f02\u6b65\u5e93\u6027\u80fd\u4f18\u5316\u4e2d\u4e0d\u53ef\u6216\u7f3a\u73af\u8282\u3002\u5728 <code>UniTask<\/code> \u4e2d\uff0c\u8fd9\u90e8\u5206\u4ee3\u7801\u5341\u5206\u7b80\u6d01\u660e\u4e86\uff0c\u56e0\u6b64 <code>STask<\/code> \u76f4\u63a5\u6cbf\u7528\u4e86\u4e0b\u6765\u3002\u5bf9\u8c61\u6c60\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5177\u4f53\u91c7\u7528\u4ec0\u4e48\u65b9\u6cd5\u9700\u8981\u89c6\u5177\u4f53\u60c5\u51b5\u3002\u8fd9\u4e00\u8282\u6240\u4ecb\u7ecd\u7684\u5bf9\u8c61\u6c60\uff0c\u975e\u5e38\u9002\u5408\u53ea\u9700\u8981\u6700\u57fa\u672c\u7684\u7f13\u5b58\u529f\u80fd\uff0c\u4e14\u4f7f\u7528\u65b9\u4fbf\u7684\u573a\u666f\uff0c\u6211\u4eec\u6ca1\u6709\u53bb\u904d\u5386\u5bf9\u8c61\u6c60\u4e2d\u7684\u5bf9\u8c61\uff0c\u6216\u8005\u5c06\u5bf9\u8c61\u6c60\u6e05\u7a7a\u8fd9\u4e9b\u5947\u5947\u602a\u602a\u7684\u9700\u6c42\uff0c\u5c0f\u4f19\u4f34\u4eec\u4e5f\u53ef\u4ee5\u5c06\u5b83\u5e94\u7528\u5728\u5176\u4ed6\u5408\u9002\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STaskCompletionSourceCore<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">STaskCompletionSourceCore&lt;TResult&gt;<\/h3>\n\n\n\n<p>\u4ece\u547d\u540d\u6765\u770b\uff0c<code>STaskCompletionSourceCore<\/code> \u7684\u7ed3\u5c3e\u5355\u8bcd &#8216;core&#8217; \uff0c\u6211\u4eec\u5c31\u80fd\u611f\u53d7\u5230\u5b83\u5728\u6574\u4e2a\u7cfb\u7edf\u4e2d\u8d77\u5230\u4e86\u4e3e\u8db3\u8f7b\u91cd\u7684\u4f5c\u7528\u3002\u8fd9\u4e2a\u7c7b\u975e\u5e38\u7c7b\u4f3c <code>ValueTask<\/code> \u6240\u7528\u5230\u7684 <a href=\"https:\/\/source.dot.net\/#System.Private.CoreLib\/src\/libraries\/System.Private.CoreLib\/src\/System\/Threading\/Tasks\/Sources\/ManualResetValueTaskSourceCore.cs,2ea1d69c971646b9\" target=\"_blank\" rel=\"noreferrer noopener\">ManualResetValueTaskSourceCore<\/a> \u3002\u6211\u4eec\u5728\u4e0a\u5c42\u8c03\u7528\u7684 <code>builder.SetResult(T)<\/code> \u3001 <code>builder.SetException(Exception)<\/code> \u548c <code>awaiter.OnCompleted(Action)<\/code> \u7b49\u65b9\u6cd5\u90fd\u4f1a\u6700\u7ec8\u6267\u884c\u5230 <code>core.TrySetResult(TResult)<\/code> \u3001 <code>core.TrySetException(Exception)<\/code> \u548c <code>core.OnCompleted(Action&lt;object>, object, token)<\/code> \u65b9\u6cd5\u91cc\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e00\u8282\u4e00\u5171\u4f1a\u4ecb\u7ecd\u56db\u4f4d\u65b0\u670b\u53cb\uff0c\u9664\u4e86 <code>STaskCompletionSourceCore<\/code> \u5916\uff0c\u8fd8\u6709\u5e2e\u5fd9\u5904\u7406\u5f02\u5e38\u7684 <code>ExceptionHolder<\/code> \u3001\u4e3a\u4e86\u6027\u80fd\u800c\u4ece\u672c\u4f53\u4e2d\u63d0\u53d6\u51fa\u6765\u5f62\u6210\u9759\u6001\u7c7b\u7684 <code>STaskCompletionSourceCoreShared<\/code> \u3001\u4ee5\u53ca\u8d1f\u8d23\u6d3e\u53d1\u5f02\u5e38\u7684 <code>STaskScheduler<\/code> \u3002\u6211\u4eec\u4f1a\u5148\u8ba8\u8bba\u524d\u4e09\u8005\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u8026\u5408\u5ea6\u6bd4\u8f83\u9ad8\uff0c\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u6574\u4f53\uff0c\u4e4b\u540e\u6211\u4eec\u518d\u6765\u8ba8\u8bba <code>STaskScheduler<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u7f16\u5199\u5728 <code>STaskCompletionSource.cs<\/code> \u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t&#91;StructLayout(LayoutKind.Auto)]\r\n    public struct STaskCompletionSourceCore&lt;TResult>\r\n    {\r\n        \/\/Struct Size: TResult + (8 + 2 + 1 + 1 + 8 + 8)\r\n        \r\n        private TResult result;\r\n        private object error;\/\/ExceptionHolder \u6216 OperationCanceledException\r\n        private short version;\r\n        private bool hasUnhandledError;\r\n        private int completedCount;\/\/0: completed == false\r\n        private Action&lt;object> continuation;\r\n        private object continuationState;\r\n\r\n        public short Version => this.version;\r\n\r\n        public void Reset()\r\n        {\r\n            this.ReportUnhandledError();\r\n\r\n            unchecked\r\n            {\r\n                this.version += 1;\/\/version \u81ea\u589e\r\n            }\r\n            this.completedCount = 0;\r\n            this.result = default;\r\n            this.error = null;\r\n            this.hasUnhandledError = false;\r\n            this.continuation = null;\r\n            this.continuationState = null;\r\n        }\r\n\r\n        private void ReportUnhandledError()\r\n        {\r\n            if (this.hasUnhandledError)\r\n            {\r\n                try\r\n                {\r\n                    if (this.error is OperationCanceledException oc)\r\n                    {\r\n                        STaskScheduler.PublishUnobservedTaskException(oc);\r\n                    }\r\n                    else if (this.error is ExceptionHolder e)\r\n                    {\r\n                        STaskScheduler.PublishUnobservedTaskException(e.GetException().SourceException);\r\n                    }\r\n                }\r\n                catch { }\r\n            }\r\n        }\r\n\r\n        internal void MarkHandled()\r\n        {\r\n            this.hasUnhandledError = false;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u6210\u529f\u6267\u884c\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"result\">&lt;\/param>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        public bool TrySetResult(TResult result)\r\n        {\r\n            \/\/if 'completedCount' is 0 before invoke Increment Method\uff0c\u53ea\u6709\u672a\u88ab\u4f7f\u7528\u8fc7\u7684\u624d\u80fd\u8bbe\u7f6e\uff0c\u5373\u53ea\u80fd\u8bbe\u7f6e\u4e00\u6b21\u7ed3\u679c\uff08\u4e0d\u8bba\u662f successful\/exception\/canceled\uff09\r\n            if (Interlocked.Increment(ref this.completedCount) == 1)\r\n            {\r\n                this.result = result;\r\n\r\n                \/\/if 'continuation' is not null (has set in OnCompleted correctly), invoke continuation,\r\n                \/\/if 'continuation' is null, set it to 's_sentinel' in order to avoid invoke it again\r\n                if (this.continuation != null || Interlocked.CompareExchange(ref this.continuation, STaskCompletionSourceCoreShared.s_sentinel, null) != null)\r\n                {\r\n                    this.continuation(this.continuationState);               \r\n                }\r\n                return true;\r\n            }\r\n\r\n            return false;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u6267\u884c\u4e2d\u9047\u5230\u5f02\u5e38\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"error\">&lt;\/param>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        public bool TrySetException(Exception error)\r\n        {\r\n            if (Interlocked.Increment(ref this.completedCount) == 1)\r\n            {\r\n                this.hasUnhandledError = true;\r\n                if (this.error is OperationCanceledException)\r\n                {\r\n                    this.error = error;\r\n                }\r\n                else\r\n                {\r\n                    this.error = new ExceptionHolder(ExceptionDispatchInfo.Capture(error));\r\n                }\r\n\r\n                if (this.continuation != null || Interlocked.CompareExchange(ref this.continuation, STaskCompletionSourceCoreShared.s_sentinel, null) != null)\r\n                {\r\n                    this.continuation(this.continuationState);\r\n                }\r\n                return true;\r\n            }\r\n\r\n            return false;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u6267\u884c\u65f6\u88ab\u53d6\u6d88\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"cancellationToken\">&lt;\/param>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        public bool TrySetCanceled(CancellationToken cancellationToken = default)\r\n        {\r\n            if (Interlocked.Increment(ref this.completedCount) == 1)\r\n            {\r\n                this.hasUnhandledError = true;\r\n                this.error = new OperationCanceledException(cancellationToken);\r\n\r\n                if (this.continuation != null || Interlocked.CompareExchange(ref this.continuation, STaskCompletionSourceCoreShared.s_sentinel, null) != null)\r\n                {\r\n                    this.continuation(this.continuationState);\r\n                }\r\n                return true;\r\n            }\r\n\r\n            return false;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u83b7\u53d6\u6267\u884c\u72b6\u6001\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"token\">&lt;\/param>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public STaskStatus GetStatus(short token)\r\n        {\r\n            this.ValidateToken(token);\r\n            return (this.continuation == null || this.completedCount == 0) ? STaskStatus.Pending\r\n                : (this.error == null) ? STaskStatus.Succeeded\r\n                : (this.error is OperationCanceledException) ? STaskStatus.Canceled\r\n                : STaskStatus.Faulted;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u4e0d\u68c0\u67e5 token \u662f\u5426\u5408\u6cd5\uff0c\u83b7\u53d6\u6267\u884c\u72b6\u6001\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public STaskStatus UnsafeGetStatus()\r\n        {\r\n            return (this.continuation == null || this.completedCount == 0) ? STaskStatus.Pending\r\n                : (this.error == null) ? STaskStatus.Succeeded\r\n                : (this.error is OperationCanceledException) ? STaskStatus.Canceled\r\n                : STaskStatus.Faulted;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u83b7\u53d6\u6267\u884c\u7ed3\u679c\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"token\"> \u6267\u884c&lt;see cref=\"STask\"\/>\u6784\u9020\u65b9\u6cd5\u65f6\u4f20\u9012\u7684\u503c &lt;\/param>\r\n        \/\/\/ &lt;returns>&lt;\/returns>\r\n        \/\/\/ &lt;exception cref=\"InvalidOperationException\">&lt;\/exception>\r\n        public TResult GetResult(short token)\r\n        {\r\n            this.ValidateToken(token);\r\n            if (this.completedCount == 0)\r\n            {\r\n                throw new InvalidOperationException(\"Not yet completed, STask only allow to ues await.\");\r\n            }\r\n\r\n            if (this.error != null)\r\n            {\r\n                this.hasUnhandledError = false;\r\n                if (this.error is OperationCanceledException oce)\r\n                {\r\n                    throw oce;\r\n                }\r\n                else if (this.error is ExceptionHolder eh)\r\n                {\r\n                    eh.GetException().Throw();\r\n                }\r\n\r\n                throw new InvalidOperationException(\"Critical: invalid exception type was held.\");\r\n            }\r\n\r\n            return this.result;\r\n        }\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u8c03\u5ea6 continuation\r\n        \/\/\/ &lt;\/summary>\r\n        \/\/\/ &lt;param name=\"continuation\">\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u88ab\u8c03\u7528\u7684\u56de\u8c03&lt;\/param>\r\n        \/\/\/ &lt;param name=\"state\">\u56de\u8c03\u7684\u53c2\u6570\uff0c\u5728 STask \u4e2d\uff0c\u5b83\u662f\u771f\u6b63\u7684 continuation\uff0c\u5177\u4f53\u53ef \u67e5\u770b STask.Awaiter.OnCompleted()&lt;\/param>\r\n        \/\/\/ &lt;param name=\"token\"> \u6267\u884c&lt;see cref=\"STask\"\/>\u6784\u9020\u65b9\u6cd5\u65f6\u4f20\u9012\u7684\u503c &lt;\/param>\r\n        \/\/\/ &lt;exception cref=\"ArgumentNullException\">continuation is null&lt;\/exception>\r\n        public void OnCompleted(Action&lt;object> continuation, object state, short token \/*, ValueTaskSourceOnCompletedFlags flags *\/)\r\n        {\r\n            if (continuation == null)\r\n            {\r\n                throw new ArgumentNullException(nameof(continuation));\r\n            }\r\n            this.ValidateToken(token);\r\n            \r\n            \/\/\u4e0d\u4f7f\u7528 ValueTaskSourceOnCompletedFlags \u5f62\u53c2\uff0c\u4e0d\u5904\u7406\u6267\u884c\u4e0a\u4e0b\u6587\u6216\u540c\u6b65\u4e0a\u4e0b\u6587\r\n            \r\n            \/\/\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u60c5\u51b5\uff1a\r\n            \/\/PatternA: GetStatus = Pending => OnCompleted => TrySet*** => GetResult\r\n            \/\/PatternB: TrySet*** => GetStatus = !Pending => GetResult  \u6b64\u65f6 this.continuation \u4e3a s_sentinel\r\n            \/\/PatternC: GetStatus = Pending => OnCompleted\/TrySet*** (race condition) => GetResult\r\n            \/\/C.1 win OnCompleted => TrySet*** invoke saved continuation\r\n            \/\/C.2 win TrySet*** => should invoke continuation here\r\n            \r\n            \/\/\u91cd\u70b9\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\r\n            \/\/\u82e5\u5148\u6267\u884c\u7684\u662f OnCompleted\uff0c\u90a3\u4e48 continuation \u4f1a\u5728\u8fd9\u91cc\u4fdd\u5b58\u4e0b\u6765\uff0c\u4f9b TrySet*** \u65b9\u6cd5\u6267\u884c\r\n            \/\/\u82e5\u5148\u6267\u884c\u7684\u662f TrySet***\uff0c\u90a3\u4e48\u5728\u8fd9\u91cc\u6267\u884c continuation\r\n\r\n            object oldContinuation = this.continuation;\r\n            if (oldContinuation == null)\/\/\u8fd8\u6ca1\u8bbe\u7f6e continuation\r\n            {\r\n                this.continuationState = state;\r\n                oldContinuation = Interlocked.CompareExchange(ref this.continuation, continuation, null);\r\n                \/\/\u6b64\u65f6 oldContinuation \u4ecd\u7136\u4e3a null\uff0cthis.continuation \u4e3a\u5f62\u53c2 continuation\r\n                \/\/\u4e4b\u540e\u4f1a\u5728 TrySet \u4e2d\u8c03\u7528 this.continuation\r\n            }\r\n\r\n            if (oldContinuation != null)\r\n            {\r\n                \/\/\u5148\u6267\u884c\u7684 TrySet\uff0c\u6b64\u65f6 oldContinuation \u4e3a s_sentinel\uff0c\u6211\u4eec\u9700\u8981\u5728\u8fd9\u91cc\u8c03\u7528\u56de\u8c03\r\n                \/\/\u82e5 oldContinuation != s_sentinel\uff0c\u8868\u793a\u591a\u6b21 await \u4e86\u540c\u4e00\u4e2a STask\uff0c\u8fd9\u662f\u4e0d\u5141\u8bb8\u7684\r\n                if (!ReferenceEquals(oldContinuation, STaskCompletionSourceCoreShared.s_sentinel))\r\n                {\r\n                    throw new InvalidOperationException(\"Already continuation registered, can not await twice or get Status after await.\");\r\n                }\r\n\r\n                continuation(state);\r\n            }\r\n        }\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        private void ValidateToken(short token)\r\n        {\r\n            if (token != this.version)\r\n            {\r\n                throw new InvalidOperationException(\"Token version is not matched, can not await twice or get Status after await.\");\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/\u4e0e\u901a\u7528\u903b\u8f91\u5206\u5f00\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\r\n    internal static class STaskCompletionSourceCoreShared\r\n    {\r\n        internal static readonly Action&lt;object> s_sentinel = CompletionSentinel; \r\n\r\n        private static void CompletionSentinel(object _)\r\n        {\r\n            throw new InvalidOperationException(\"The sentinel delegate should never be invoked.\");\r\n        }\r\n    }\r\n\r\n    internal class ExceptionHolder\r\n    {\r\n        private ExceptionDispatchInfo exception;\r\n        private bool calledGet = false;\r\n\r\n        public ExceptionHolder(ExceptionDispatchInfo exception)\r\n        {\r\n            this.exception = exception;\r\n        }\r\n\r\n        public ExceptionDispatchInfo GetException()\r\n        {\r\n            if (!this.calledGet)\r\n            {\r\n                this.calledGet = true;\r\n                GC.SuppressFinalize(this);\/\/\u901a\u77e5GC\u4e0d\u518d\u8c03\u7528\u6790\u6784\u51fd\u6570\uff0c\u9632\u6b62\u5728Finalize\u7ebf\u7a0b\u8c03\u7528\u6790\u6784\u51fd\u6570\u4e4b\u540e\uff0cGC\u518d\u91cd\u590d\u8c03\u7528\r\n            }\r\n            return this.exception;\r\n        }\r\n\r\n        ~ExceptionHolder()\r\n        {\r\n            if (!this.calledGet)\r\n            {\r\n                STaskScheduler.PublishUnobservedTaskException(this.exception.SourceException);\r\n            }\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u5728\u6ce8\u91ca\u4e2d\uff0c\u6807\u51fa\u4e86 <code>STaskCompletionSourceCore<\/code> \u6210\u5458\u53d8\u91cf\u6240\u5360\u7528\u7684\u5b57\u8282\u5927\u5c0f\uff0c\u5b83\u4eec\u5728\u7ed3\u6784\u4e2d\u7684\u4f5c\u7528\u5206\u522b\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>result<\/code> \uff1a\u5f02\u6b65\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff0c\u5373\u8c03\u7528\u4f7f\u7528 <code>await<\/code> \u5173\u952e\u5b57\u540e\u7684\u8fd4\u56de\u503c\u3002<\/li>\n\n\n\n<li><code>error<\/code> \uff1a\u9047\u5230\u5f02\u5e38\u540e\u6216\u7b49\u5f85\u88ab\u53d6\u6d88\u540e\uff0c\u4f1a\u8c03\u7528 <code>TrySetException<\/code> \/ <code>TrySetCanceled<\/code> \u5e76\u5c06\u5f02\u5e38\u5bf9\u8c61\u8d4b\u503c\u7ed9\u5b83\uff0c\u56e0\u6b64\u5b83\u53ef\u80fd\u666e\u901a\u5f02\u5e38\u5bf9\u8c61\u5305\u88c5\u540e\u7684 <code>ExceptionHolder<\/code> \u6216\u53d6\u6d88\u5f02\u5e38 <code>OperationCanceledException<\/code> \u3002\u6211\u4eec\u9700\u8981\u5c06\u53d6\u6d88\u5f02\u5e38\u4e0e\u5176\u4ed6\u5f02\u5e38\u8fdb\u884c\u533a\u5206\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u8bc6\u522b <code>STask<\/code> \u7684\u5177\u4f53\u8fd0\u884c\u72b6\u6001\u3002<\/li>\n\n\n\n<li><code>version<\/code> \uff1a\u7528\u4e8e\u6807\u8bc6\u5f53\u524d <code>STaskCompletionSourceCore<\/code> \u5bf9\u8c61\u7684 &#8216;token&#8217;\u3002<\/li>\n\n\n\n<li><code>hasUnhandledError<\/code> \uff1a\u9047\u5230\u5f02\u5e38\u540e\uff0c\u6211\u4eec\u4f1a\u5c06\u5b83\u8bbe\u7f6e\u4e3a <code>true<\/code> \uff0c\u5728 <code>core<\/code> \u7ed3\u675f\u5f53\u524d\u7684\u5bff\u547d\u5468\u671f\u524d\uff0c\u6211\u4eec\u4f1a\u901a\u8fc7\u5b83\u6765\u5224\u65ad\u662f\u5426\u6709\u5f02\u5e38\uff0c\u82e5\u6709\u5219\u9700\u8981\u5c06\u5f02\u5e38\u6d3e\u53d1\u51fa\u6765\u3002<\/li>\n\n\n\n<li><code>completedCount<\/code> \uff1a\u6211\u4eec\u901a\u8fc7\u5b83\u6765\u5224\u65ad <code>core<\/code> \u7684\u5de5\u4f5c\u6709\u6ca1\u6709\u5b8c\u6210\u3002\u5f53\u4e0a\u5c42\u8c03\u7528 <code>TrySet***<\/code> \u7684\u65b9\u6cd5\u540e\uff0c\u5c31\u8868\u793a <code>core<\/code> \u7684\u5de5\u4f5c\u5b8c\u6210\u4e86\u3002<\/li>\n\n\n\n<li><code>Action&lt;object&gt;<\/code> \uff1a\u7b49\u5f85\u5b8c\u6210\u540e\u7684\u56de\u8c03\u59d4\u6258\u3002<\/li>\n\n\n\n<li><code>continuationState<\/code> \uff1a\u56de\u8c03\u59d4\u6258\u7684\u53c2\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662f <code>version<\/code> \u53d8\u91cf\u3002<code>STask<\/code> \u7684\u6bcf\u6b21\u7b49\u5f85\u64cd\u4f5c\uff0c\u5e95\u5c42\u90fd\u662f\u7531 <code>core<\/code> \u6765\u5b8c\u6210\u7684\u3002\u524d\u9762\u6211\u4eec\u4e5f\u63d0\u5230\uff0c\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5bf9\u8c61\u6c60\uff0c\u800c <code>core<\/code> \u5c31\u662f\u6211\u4eec\u8981\u201c\u6c60\u5316\u201d\u7684\u5bf9\u8c61\u3002\u4f46\u95ee\u9898\u6765\u4e86\uff1a\u7531\u4e8e <code>core<\/code> \u662f\u53ef\u88ab\u590d\u7528\u7684\uff0c\u5f53\u7528\u6237\u6ca1\u6709\u76f4\u63a5 <code>await<\/code> \u5f02\u6b65\u65b9\u6cd5\uff0c\u800c\u662f\u5c06\u5f02\u6b65\u65b9\u6cd5\u8fd4\u56de\u7684 <code>STask<\/code> \u7ed9\u4fdd\u5b58\u4e0b\u6765\uff0c\u5e76\u5728\u540e\u7eed\u591a\u6b21\u7b49\u5f85\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STask task = MethodAsync();\r\nawait task;\r\nawait task;\/\/\u4e0d\u5141\u8bb8\uff0c\u629b\u51fa\u5f02\u5e38<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u7684\u524d\u4e24\u884c\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u7b2c\u4e09\u884c\u5bf9\u540c\u4e00 <code>STask<\/code> \u7684\u591a\u6b21\u7b49\u5f85\uff0c\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\u3002\u5728 <code>core<\/code> \u7ed3\u6784\u7684\u903b\u8f91\u4e2d\uff0c\u5f53\u5b83\u88ab\u91cd\u65b0\u653e\u5165\u5bf9\u8c61\u6c60\uff0c\u9700\u8981\u5c06\u6210\u5458\u53d8\u91cf\u90fd\u91cd\u7f6e\u4e00\u904d\uff0c\u4ee5\u4fbf\u4e0b\u6b21\u4f7f\u7528\uff0c\u8fd9\u91cc\u7684\u6210\u5458\u53d8\u91cf\u5c31\u5305\u62ec\u4e86\u8fd4\u56de\u503c\u3002\u5728 STask \u5f02\u6b65\u5e93\u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u72b6\u6001\u673a\u8c03\u7528 <code>awaiter.GetResult<\/code> \u65f6\uff0c\u5728\u5e95\u5c42\u8c03\u7528 <code>core.GetResult<\/code> \uff0c\u7136\u540e\u91cd\u7f6e <code>core<\/code> \u5bf9\u8c61\u5e76\u653e\u5165\u5bf9\u8c61\u6c60\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskCompletionSource0-1024x480.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"480\" data-original=\"https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskCompletionSource0-1024x480.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1312\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u4ece\u6d41\u7a0b\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u7b2c\u4e00\u6b21 <code>await<\/code> \u540e\uff0c\u518d\u6b21 <code>await<\/code> \uff0c\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u80fd\u5f97\u5230\u548c\u7b2c\u4e00\u6b21\u4e00\u81f4\u7684\u7ed3\u679c\uff08\u82e5 STask \u6709\u8fd4\u56de\u503c\uff09\uff0c\u56e0\u4e3a <code>core<\/code> \u5df2\u7ecf\u88ab\u91cd\u7f6e\u5e76\u56de\u6536\u4e86\u3002\u8fd9\u4e5f\u662f\u4f7f\u7528\u4f7f\u7528 <code>STask<\/code> \u7684\u9650\u5236\u6240\u5728\uff0c<strong>\u4e0d\u8981\u591a\u6b21 <code>await<\/code> \u540c\u4e00\u4e2a <code>STask<\/code><\/strong> \u3002\u5b9e\u9645\u4e0a\uff0c.NET \u63d0\u4f9b\u7684 <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.threading.tasks.valuetask-1?view=netcore-3.1\" target=\"_blank\" rel=\"noreferrer noopener\">ValueTask<\/a> \u4e5f\u6709\u8fd9\u6837\u7684\u9650\u5236\uff0c\u5b83\u4eec\u5185\u90e8\u90fd\u91c7\u7528 <code>version<\/code> \u6765\u5224\u65ad\u5f53\u524d\u7684 <code>core<\/code> \u662f\u5426\u5728\u6709\u6548\u671f\u5185\uff08\u540c\u6837\u7684\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a <code>STask<\/code> \u662f\u5426\u6709\u6548\uff0c\u5728\u5b83\u4fe9\u7684\u751f\u547d\u5468\u671f\u4e2d\uff0c<code>version<\/code> \u76f8\u7b49\uff09\u3002\u4e3a\u4e86\u9650\u5236\u7528\u6237\u591a\u6b21\u7b49\u5f85\uff0c\u6211\u4eec\u5728 <code>core<\/code> \u5185\u90e8\u7684\u4e00\u4e9b\u5730\u65b9\u589e\u52a0\u4e86\u5224\u65ad\u5f53\u524d <code>version<\/code> \u7684\u6b65\u9aa4\uff08<code>ValidateToken<\/code> \u65b9\u6cd5)\u3002\u5f53\u9700\u8981\u5bf9 <code>core<\/code> \u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u4ece\u5916\u90e8\u4f20\u5165 token\u2014\u2014\u4e5f\u5c31\u662f\u7533\u8bf7 <code>core<\/code> \u65f6\u5b83\u7684 <code>version<\/code> \u7684\u503c\u6765\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u8ba8\u8bba <code>core<\/code> \u62e5\u6709\u7684\u6210\u5458\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Reset<\/code> \uff1a\u4e0a\u9762\u63d0\u5230\u7684\uff0c\u5728 <code>core<\/code> \u88ab\u56de\u6536\u8fdb\u6c60\u524d\u9700\u8981\u91cd\u7f6e\u6210\u5458\u53d8\u91cf\uff0c\u201c\u91cd\u7f6e\u201d\u8fd9\u4e00\u6b65\u9aa4\u5c31\u662f\u8c03\u7528 <code>Reset<\/code> \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u5148\u8c03\u7528 <code>ReportUnhandledError<\/code> \u5904\u7406\u5f02\u5e38\uff0c\u7136\u540e\u5c06 <code>version<\/code> \u7684\u503c\u81ea\u589e 1\uff0c\u628a\u5176\u4f59\u7684\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002\u4e3a\u4e86\u9632\u6b62\u81ea\u589e <code>version<\/code> \u53d1\u751f\u6ea2\u51fa\uff0c\u6211\u4eec\u7528 <code>unchecked<\/code> \u8bed\u53e5\u5c06\u5b83\u6846\u8d77\u6765\u3002<\/li>\n\n\n\n<li><code>ReportUnhandledError<\/code> \uff1a\u5c06\u5b58\u5728\u7684\u5f02\u5e38\u6d3e\u53d1\u51fa\u53bb\uff0c\u91cc\u9762\u7528\u5230\u7684 <code>STaskScheduler<\/code> \u4f1a\u5728\u4e4b\u540e\u8ba8\u8bba\u3002<\/li>\n\n\n\n<li><code>MarkHandled<\/code> \uff1a\u63d0\u4f9b\u6807\u8bb0\u5f02\u5e38\u5df2\u89e3\u51b3\u7684\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><code>TrySetResult<\/code> \uff1a\u5f02\u6b65\u65b9\u6cd5\u6210\u529f\u6267\u884c\uff0c\u5c06\u6267\u884c\u7ed3\u679c\u8d4b\u503c\u7ed9 <code>result<\/code> \uff0c\u7136\u540e\u89c6\u60c5\u51b5\u8c03\u7528 <code>continuation<\/code> \uff0c\u5373 <code>await<\/code> \u8bed\u53e5\u4e4b\u540e\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><code>TrySetException<\/code> \uff1a\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u4e2d\u9047\u5230\u5f02\u5e38\uff0c\u5c06\u5f02\u5e38\u5bf9\u8c61\u8d4b\u503c\u7ed9 <code>error<\/code> \uff0c\u7136\u540e\u89c6\u60c5\u51b5\u8c03\u7528 <code>continuation<\/code> \u3002<\/li>\n\n\n\n<li><code>TrySetCanceled<\/code> \uff1a\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u4e2d\u88ab\u53d6\u6d88\uff0c\u7533\u8bf7\u4e00\u4e2a <code>OperationCanceledException<\/code> \u5bf9\u8c61\uff0c\u5c06\u5176\u8d4b\u503c\u7ed9 <code>error<\/code> \uff0c\u6700\u540e\u89c6\u60c5\u51b5\u8c03\u7528 <code>continuation<\/code> \u3002<\/li>\n\n\n\n<li><code>GetStatus<\/code> \uff1a\u83b7\u53d6\u5f53\u524d\u6267\u884c\u72b6\u6001\uff0c\u8c03\u7528 <code>awaiter.IsCompleted<\/code> \u65f6\u4f1a\u6267\u884c\u5230\u8be5\u65b9\u6cd5\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\uff09\u3002<\/li>\n\n\n\n<li><code>UnsafeGetStatus<\/code> \uff1a\u4e5f\u662f\u83b7\u53d6\u5f53\u524d\u8fd0\u884c\u72b6\u6001\uff0c\u4e0d\u68c0\u67e5 token \u662f\u5426\u5408\u6cd5\u3002\u8be5\u65b9\u6cd5\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\uff0c\u5e73\u65f6\u7528\u4e0d\u5230\u3002<\/li>\n\n\n\n<li><code>GetResult<\/code> \uff1a\u83b7\u53d6\u5f53\u524d\u7684\u6267\u884c\u7ed3\u679c\u3002\u8fd4\u56de\u7ed3\u679c\u524d\uff0c\u4f1a\u8fdb\u884c\u4e00\u7cfb\u5217\u5408\u6cd5\u6027\u5224\u65ad\uff1a <code>version<\/code> \u53d8\u91cf\u3001\u65b9\u6cd5\u662f\u5426\u6267\u884c\u5b8c\u6bd5\u3001\u662f\u5426\u6709\u672a\u5904\u7406\u5f02\u5e38\u3002\u8c03\u7528 <code>awaiter.GetResult<\/code> \u65f6\u4f1a\u6267\u884c\u5230\u8be5\u65b9\u6cd5\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\uff09\u3002<\/li>\n\n\n\n<li><code>ValidateToken<\/code> \uff1a\u5224\u65ad token \u5408\u6cd5\u6027\u3002<\/li>\n\n\n\n<li><code>OnCompleted<\/code> \uff1a\u5904\u7406 <code>await<\/code> \u8bed\u53e5\u4e4b\u540e\u7684\u56de\u8c03\u3002\u8be5\u65b9\u6cd5\u4e0e <a href=\"https:\/\/source.dot.net\/#System.Private.CoreLib\/src\/libraries\/System.Private.CoreLib\/src\/System\/Threading\/Tasks\/Sources\/ManualResetValueTaskSourceCore.cs,120\" target=\"_blank\" rel=\"noreferrer noopener\">ManualResetValueTaskSource.OnCompleted<\/a> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u8c03\u7528 <code>awaiter.OnCompleted<\/code> \u65f6\u4f1a\u6267\u884c\u5230\u8be5\u65b9\u6cd5\uff0c\u4f46\u53bb\u6389\u4e86\u5904\u7406\u4e0a\u4e0b\u6587\u7684\u903b\u8f91\u2014\u2014\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\uff0c\u4ee5\u6b64\u4f18\u5316\u6027\u80fd\u3002\u8be5\u65b9\u6cd5\u867d\u7136\u4ee3\u7801\u91cf\u4e0d\u5927\uff0c\u5374\u662f <code>STaskCompletionSourceCore<\/code> \u4e2d\u6700\u96be\u7406\u89e3\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u62ff\u5230\u56de\u8c03\u65b9\u6cd5\u540e\u5c31\u76f4\u63a5\u8c03\u7528\uff0c\u8c03\u7528\u56de\u8c03\u65b9\u6cd5\u7684\u65f6\u673a\u662f\u6709\u70b9\u5c0f\u8bb2\u7a76\u7684\uff0c\u8fd9\u91cc\u4e0e\u4e0a\u8ff0\u4e09\u4e2a <code>TrySet***<\/code> \u65b9\u6cd5\u6709\u5bc6\u5207\u7684\u5173\u8054\uff0c\u4e0b\u9762\u6211\u4eec\u628a\u5b83\u4eec\u653e\u5728\u4e00\u8d77\u8ba8\u8bba\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5047\u5982\u6709\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public async STask&lt;float> MethodAsync()\r\n{\r\n    float thisTime = Time.realtimeSinceStartup;\r\n    await STask.Delay(5000);\/\/\u7b49\u5f855\u79d2\r\n    return Time.realtimeSinceStartup - thisTime;\r\n}\r\n\r\npublic void Start()\r\n{\r\n    float awaitTime = await MethodAsync();\r\n    Debug.Log($\"consume time: {awaitTime}s\");\r\n}<\/code><\/pre>\n\n\n\n<p>\u89c2\u5bdf\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u6211\u4eec\u6709\u4e24\u4e2a STask:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>MethodAsync \u65b9\u6cd5\u7684\u8fd4\u56de\u503c <code>STask&lt;float&gt;<\/code> \u3002<\/li>\n\n\n\n<li>STask.Delay \u65b9\u6cd5\u7684\u8fd4\u56de\u503c STask \u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5176\u4e2d\u53ea\u6709\u7b2c\u4e00\u4e2a STask&lt;float&gt; \u662f\u4f5c\u4e3a<strong>\u5f02\u6b65\u65b9\u6cd5<\/strong>\u7684\u4e00\u90e8\u5206\u5b58\u5728\u7684\uff0c\u56e0\u6b64\u5b83\u4f1a\u8d70\u4e0e\u4e4b\u5173\u8054\u7684\u5f02\u6b65\u72b6\u6001\u673a\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u72b6\u6001\u673a\u3002<\/p>\n\n\n\n<p>\u800c\u7b2c\u4e8c\u4e2a STask \u5e76\u4e0d\u662f\u5f02\u6b65\u65b9\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u4e3a STask.Delay \u65b9\u6cd5\u5e76\u6ca1\u6709\u7528 async \u5173\u952e\u5b57\u6807\u8bb0\uff0c\u5176\u5185\u90e8\u4e5f\u5c31\u6ca1\u6709 await \u8bed\u53e5\uff0c\u56e0\u6b64\u5b83\u5c31\u4e0d\u4f1a\u8d70\u81ea\u5b9a\u4e49\u72b6\u6001\u673a\u3002\uff08\u5173\u4e8e STask.Delay \u5b9e\u73b0\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8ba8\u8bba\uff0c\u5927\u4f19\u5728\u8fd9\u91cc\u53ea\u9700\u8981\u5176\u5185\u90e8\u4f9d\u9760\u7684\u662f Unity \u63d0\u4f9b\u7684 PlayerLoop System \u5b9e\u73b0\u7684\uff0c\u4e0d\u6d89\u53ca\u591a\u7ebf\u7a0b\uff09\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b\u8fd9\u6bb5\u4ee3\u7801\u7684\u5f02\u6b65\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskCompletionSource1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"887\" height=\"624\" data-original=\"https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskCompletionSource1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1313\"  sizes=\"auto, (max-width: 887px) 100vw, 887px\" \/><\/div><\/figure>\n\n\n\n<p>\u4e0a\u56fe\u6f14\u793a\u4e86\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u65f6\u7684<strong>\u5927\u81f4<\/strong>\u6d41\u7a0b\uff08\u5b9e\u7ebf\u7bad\u5934\u8868\u793a\u6d88\u606f\u53d1\u51fa\uff0c\u865a\u7ebf\u7bad\u5934\u8868\u793a\u6d88\u606f\u8fd4\u56de\uff09\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f02\u6b65\u65b9\u6cd5 <code>MethodAsync<\/code> \u6267\u884c\u5230 await \u8bed\u53e5\uff0c\u72b6\u6001\u673a\u4f1a\u81ea\u52a8\u8c03\u7528\u88ab\u7b49\u5f85\u5bf9\u8c61\u7684 <code>GetAwaiter<\/code> \u65b9\u6cd5\u83b7\u53d6 <code>Awaiter<\/code> \uff0c\u5176\u4e2d\u88ab\u7b49\u5f85\u5bf9\u8c61\u662f <code>STask.Delay<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684 <code>STask<\/code> \u3002\u5728\u72b6\u6001\u673a\u83b7\u53d6\u5230 <code>Awaiter<\/code> \u65f6\uff0c\u5df2\u7ecf\u8d70\u8fc7\u4e0a\u56fe\u7684\u6b65\u9aa4\u2460\u5230\u6b65\u9aa4\u2463\u3002<\/li>\n\n\n\n<li>\u7136\u540e\uff0c\u72b6\u6001\u673a\u4f1a\u901a\u8fc7 <code>Awaiter.IsCompleted<\/code> \u5b57\u6bb5\u5224\u65ad\u88ab\u7b49\u5f85\u5bf9\u8c61\uff08\u5373 <code>STask<\/code> \uff09\u662f\u5426\u5b8c\u6210\u4e86\u5de5\u4f5c\u3002\u82e5\u5b8c\u6210\uff0c\u5219\u8c03\u7528 <code>Awaiter.GetResult<\/code> \u83b7\u53d6\u7ed3\u679c\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9 await \u8bed\u53e5\u7684\u5de6\u503c\uff08\u4e0a\u9762\u793a\u4f8b\u4ee3\u7801\u7684 <code>STask<\/code> \u662f\u65e0\u8fd4\u56de\u503c\u7684\uff0c\u4f46\u4e5f\u4f1a\u6267\u884c\u201c\u83b7\u53d6\u7ed3\u679c\u201d\u7684\u6b65\u9aa4\uff0c\u53ea\u4e0d\u8fc7\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\uff09\u3002\u82e5\u6ca1\u6709\u5b8c\u6210\uff0c\u5219\u8c03\u7528 <code>AwaitOnCompleted<\/code> \uff0c\u628a await \u8bed\u53e5\u540e\u9762\u7684\u4ee3\u7801\u6253\u5305\u6210\u56de\u8c03\u65b9\u6cd5\uff0c\u544a\u8bc9 <code>Awaiter<\/code> \uff0c\u610f\u601d\u662f\u5f53 <code>Awaiter<\/code> \u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u8c03\u7528\u8be5\u56de\u8c03\uff0c\u8fbe\u5230\u7ee7\u7eed\u6267\u884c\u540e\u9762\u4ee3\u7801\u7684\u6548\u679c\u3002\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u6709\u7591\u60d1\uff0c\u72b6\u6001\u673a\u9664\u4e86\u6709 <code>AwaitOnCompleted<\/code> \u65b9\u6cd5\u5916\uff0c\u8fd8\u6709 <code>AwaitUnsafeOnCompleted<\/code> \uff0c\u5b83\u4eec\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f\u5f53 <code>Awaiter<\/code> \u5b9e\u73b0\u7684\u662f <code>INotifyCompletion<\/code> \u63a5\u53e3\u65f6\uff0c\u72b6\u6001\u673a\u5c06\u8c03\u7528\u524d\u8005\uff0c\u5b9e\u73b0\u7684\u662f <code>ICriticalNotifyCompletion<\/code> \u65f6\uff0c\u8c03\u7528\u7684\u662f\u540e\u8005\uff0c\u5177\u4f53\u53ef\u53c2\u8003\uff1a<a href=\"https:\/\/stackoverflow.com\/questions\/65529509\/what-is-icriticalnotifycompletion-for\" target=\"_blank\" rel=\"noreferrer noopener\">What is ICriticalNotifyCompletion for?<\/a> \u3002\u5230\u6b64\uff0c\u6211\u4eec\u6267\u884c\u4e86\u4e0a\u56fe\u7684\u6b65\u9aa4\u2464\u5230\u6b65\u9aa4\u2465\u3002<\/li>\n\n\n\n<li>STask \u7684\u5e95\u5c42\u6838\u5fc3\u673a\u5236\u90fd\u7531 <code>STaskCompletionSourceCore<\/code> \u6765\u627f\u62c5\uff0c\u56e0\u6b64 <code>Awaiter<\/code> \u4f1a\u5c06\u56de\u8c03\u4f20\u9012\u7ed9 <code>core<\/code> \uff0c\u5373\u6267\u884c\u6b65\u9aa4\u2466\u3001\u2467\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e0a\u9762\u7684\u6d41\u7a0b\u90fd\u662f\u6309\u7167\u5148\u540e\u987a\u5e8f\u6267\u884c\u7684\u3002\u5728\u6b65\u9aa4\u2461\uff0c\u5f53 <code>STask.Delay<\/code> \u521b\u5efa <code>STask<\/code> \u5bf9\u8c61\u65f6\uff0c<code>STas<\/code> \u5185\u90e8\u4e5f\u521b\u5efa\u4e86 <code>DelayPromise<\/code> \uff0c\u5b83\u662f\u5b9e\u73b0\u5ef6\u8fdf\u529f\u80fd\u7684\u5bf9\u8c61\uff0c\u5b9e\u73b0\u4e86 ISTaskSource \u63a5\u53e3\u3002\u5f53\u8bbe\u7f6e\u7684\u5ef6\u8fdf\u65f6\u95f4\u7ed3\u675f\uff0c <code>DelayPromise<\/code> \u4f1a\u8c03\u7528 <code>TrySetResult<\/code> \/ <code>TrySetException<\/code> \u901a\u77e5 <code>STaskCompletionSourceCore<\/code> \u4efb\u52a1\u7ed3\u675f\u3002\u5728\u8fd9\u91cc\uff0c\u6210\u529f\u3001\u5931\u8d25\u6216\u8005\u53d6\u6d88\u90fd\u89c6\u4f5c\u4efb\u52a1\u7ed3\u675f\uff0c\u56e0\u6b64\u6211\u4eec\u5728\u540e\u9762\u5c06\u4efb\u52a1\u7ed3\u675f\u540e\u8c03\u7528\u7684\u65b9\u6cd5\u7edf\u79f0\u4e3a <code>TrySet***<\/code> \u3002<\/p>\n\n\n\n<p>\u68b3\u7406\u5b8c\u6d41\u7a0b\uff0c\u73b0\u5728\u6211\u4eec\u8fd8\u5269\u4e0b\u4e00\u4e2a\u6700\u5173\u952e\u7684\u95ee\u9898\uff0c\u4ec0\u4e48\u65f6\u5019\u53bb\u8c03\u7528\u56de\u8c03\u65b9\u6cd5\uff1f<\/p>\n\n\n\n<p>\u5f53\u7136\u662f\u5728\u5f02\u6b65\u4efb\u52a1\u7ed3\u675f\u65f6\u8c03\u7528\u5566\uff01\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53 <code>TrySet***<\/code> \u88ab\u8c03\u7528\u65f6\uff0c\u6211\u4eec\u5c31\u8be5\u8c03\u7528\u56de\u8c03\u65b9\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u4f46\u771f\u662f\u5982\u6b64\u7b80\u5355\u5417\uff1f\u52a8\u52a8\u6211\u4eec\u7684\u5c0f\u8111\u74dc\u60f3\u60f3\uff0c\u96be\u9053 <code>TrySet***<\/code> \u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u65f6\u673a\uff0c\u4e00\u5b9a\u5728 <code>core.OnCompleted<\/code> \u4e4b\u540e\u5417\uff1f<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\uff0c\u4e00\u5207\u7684\u4e00\u5207\u90fd\u5f52\u6839\u5230\u4e24\u4e2a\u5b57\u2014\u2014\u201c\u5f02\u6b65\u201d\u3002\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u77e5\u9053\u4efb\u52a1\u6267\u884c\u9700\u8981\u7684\u65f6\u95f4\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\uff09\uff0c\u4e5f\u65e0\u6cd5\u9884\u6d4b\u4efb\u52a1\u662f\u5426\u4f1a\u88ab\u53d6\u6d88\u6216\u8005\u9047\u5230\u5f02\u5e38\uff0c\u56e0\u6b64<strong>\u6211\u4eec\u65e0\u6cd5\u5224\u65ad <code>TrySet***<\/code> \u88ab\u8c03\u7528\u7684\u51c6\u786e\u65f6\u673a<\/strong>\u3002<\/p>\n\n\n\n<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u5148\u6267\u884c <code>core.OnCompleted<\/code> \u65b9\u6cd5\uff0c\u628a\u56de\u8c03\u65b9\u6cd5\u4f20\u9012\u7ed9 <code>core<\/code> \uff0c\u7136\u540e\u7b49\u5f85\u4efb\u52a1\u7ed3\u675f\u8c03\u7528 <code>core.TrySet***<\/code> \uff0c\u5e76\u5728 <code>TrySet***<\/code> \u4e2d\u8c03\u7528\u56de\u8c03\u3002\u4f46\u73b0\u5b9e\u6709\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>core.GetStatus<\/code> = Pending \u2192 <code>core.Complted<\/code> \u2192 <code>core.TrySet***<\/code> \u2192 <code>core.GetResult<\/code> \uff1a\u6700\u5bb9\u6613\u7406\u89e3\u7684\u60c5\u51b5\uff0c\u5373\u4e0a\u9762\u63d0\u5230\u7684\u7406\u60f3\u60c5\u51b5\u3002<\/li>\n\n\n\n<li><code>TrySet***<\/code> \u2192 <code>GetStatus<\/code> != Pending \u2192 <code>GetResult<\/code> \uff1a\u5728\u5f02\u6b65\u72b6\u6001\u673a\u7b49\u5f85\u524d\uff0c\u4efb\u52a1\u5c31\u5df2\u7ecf\u7ed3\u675f\u4e86\uff0c\u76f4\u63a5\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>GetStatus<\/code> = Pending \u2192 <code>OnComplted<\/code> \/ <code>TrySet***<\/code> \u2192 <code>GetResult<\/code> \uff1a\u7b2c\u4e00\u79cd\u60c5\u51b5\u7684\u8d85\u96c6\uff0c\u5728\u7b49\u5f85\u4efb\u52a1\u65f6\uff0c<code>OnComplted<\/code> \u548c <code>TrySet***<\/code> \u5904\u4e8e\u201c\u7ade\u4e89\u201d\u5173\u7cfb\u4e2d\u2014\u2014\u5b83\u4eec\u4efb\u4f55\u4e00\u4e2a\u90fd\u53ef\u80fd\u4f1a\u6bd4\u53e6\u4e00\u4e2a\u5148\u88ab\u8c03\u7528\u3002\u5176\u5b9e\uff0c\u7b2c\u4e00\u79cd\u60c5\u51b5\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u5c0f\u4f19\u4f34\u4eec\u7406\u89e3\uff0c\u4ece\u8fd9\u91cc\u62c6\u5206\u51fa\u53bb\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u7684\u95ee\u9898\u53d8\u6210\u4e86\u600e\u6837\u5728 <code>OnComplted<\/code> \u548c <code>TrySet***<\/code> \u7684\u7ade\u4e89\u5173\u7cfb\u4e2d\uff0c\u5904\u7406\u5f02\u6b65\u65b9\u6cd5\u7684\u56de\u8c03\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5148\u8c03\u7528\u4e86 <code>OnComplted<\/code> \uff0c\u5f02\u6b65\u4efb\u52a1\u8fd8\u672a\u7ed3\u675f\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u4f20\u8fdb\u6765\u7684\u56de\u8c03\u65b9\u6cd5\u4fdd\u5b58\u4e0b\u6765\uff0c\u4f9b <code>TrySet***<\/code> \u5728\u540e\u9762\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u5148\u8c03\u7528\u4e86 <code>TrySet***<\/code> \uff0c\u5f02\u6b65\u4efb\u52a1\u5df2\u7ed3\u675f\uff0c\u90a3\u4e48\u76f4\u63a5\u6267\u884c\u56de\u8c03\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n\n\n\n<p><code>STaskCompletionSourceCore<\/code> \u6700\u6838\u5fc3\u7684\u903b\u8f91\u5c31\u662f\u8fd9\u4e48\u591a\uff0c\u5177\u4f53\u7684\u4ee3\u7801\u5df2\u7ecf\u5728\u4e0a\u9762\u7ed9\u51fa\u4e86\uff0c\u5e76\u4e14\u5fc5\u8981\u7684\u5730\u65b9\u5df2\u7ecf\u505a\u597d\u4e86\u6ce8\u91ca\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u9010\u884c\u5206\u6790\u4e86\u54df~<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExceptionHolder<\/h3>\n\n\n\n<p>\u5728 <code>STaskCompletionSourceCore<\/code> \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7c7b <code>ExceptionHolder<\/code> \u6765\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u8fd9\u6837\u505a\u7684\u76ee\u7684\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06 <code>OperationCanceledException<\/code> \uff08\u540e\u6587\u5c06\u5176\u7b80\u79f0\u4e3a\u201cOCE\u201d\uff09\u4e0e\u5176\u4ed6\u5f02\u5e38\u533a\u5206\u5f00\u3002\u5728C#\u4e2d\uff0c\u60f3\u4f18\u96c5\u5730\u53d6\u6d88\u67d0\u4e2a\u64cd\u4f5c\uff0c\u6211\u4eec\u4e00\u822c\u901a\u8fc7\u89e6\u53d1\u201c\u64cd\u4f5c\u53d6\u6d88\u201d\u5f02\u5e38\uff0c\u5373 \u201cOCE\u201d \u6765\u5b8c\u6210\u3002\u5e76\u4e14\u5728\u5904\u7406\u65f6\uff0c\u201cOCE\u201d \u4e0e\u5176\u5b83\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\u901a\u5e38\u4e5f\u6709\u533a\u522b\uff0c\u56e0\u6b64\u6211\u4eec\u6700\u597d\u5c06\u5b83\u4ece <code>Exception<\/code> \u5927\u7c7b\u4e2d\u62bd\u79bb\u51fa\u6765\uff0c\u800c\u5269\u4e0b\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u6211\u4eec\u5c31\u7528 <code>ExceptionHolder<\/code> \u5305\u88c5\u8d77\u6765\uff0c\u8fd9\u6837\u5c31\u53ea\u6709\u4e24\u79cd\u5f02\u5e38\u4e86\uff08\u5bf9\u4e8e <code>STaskCompletionSourceCore<\/code> \u6765\u8bf4\uff09\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u4e0d\u4f1a\u9057\u6f0f\u672a\u5904\u7406\u7684\u5f02\u5e38\u3002\u6211\u4eec\u4f1a\u5728 <code>ExceptionHolder<\/code> \u7684\u6790\u6784\u65b9\u6cd5\u4e2d\u68c0\u6d4b\u6240\u5305\u88c5\u7684\u5f02\u5e38\u5bf9\u8c61\u662f\u5426\u88ab\u5904\u7406\u8fc7\uff0c\u82e5\u6ca1\u6709\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b83\u629b\u51fa\u3002\u5f53\u5916\u90e8\u901a\u8fc7 <code>ExceptionHolder.GetException<\/code> \u65b9\u6cd5\u83b7\u53d6\u4e86\u5f02\u5e38\u5bf9\u8c61\uff0c\u6211\u4eec\u5c31\u8ba4\u4e3a\u5f02\u5e38\u5df2\u88ab\u5904\u7406\uff0c\u901a\u8fc7 <code>GC.SuppressFinalize<\/code> \u63a5\u53e3\u901a\u77e5\u5783\u573e\u56de\u6536\u673a\u5236\u4e0d\u518d\u8c03\u7528\u6790\u6784\u65b9\u6cd5\uff0c\u9632\u6b62\u629b\u51fa\u4e0d\u8be5\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">STaskCompletionSourceCoreShared<\/h3>\n\n\n\n<p>\u7531\u4e8e STask \u662f\u4e0d\u5141\u8bb8\u88ab\u591a\u6b21\u7b49\u5f85\u7684\u2014\u2014\u8fd9\u6837\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7528\u6237\u5f97\u5230\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u7528\u6237\u591a\u6b21\u4f7f\u7528 <code>await<\/code> \u8bed\u53e5\u7b49\u5f85\u540c\u4e00 STask \u65f6\uff0c\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5728\u54ea\u91cc\u68c0\u6d4b STask \u662f\u5426\u88ab\u591a\u6b21\u7b49\u5f85\u4e86\u5462\uff1f\u5f53\u7528\u6237\u7b49\u5f85 STask \u65f6\uff0c\u4ee3\u7801\u6700\u7ec8\u4f1a\u6267\u884c\u5230 <code>core.OnComplted<\/code> \u548c <code>TrySet***<\/code> \u65b9\u6cd5\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u91cc\u9762\u6dfb\u52a0\u68c0\u6d4b\u4ee3\u7801\uff0c\u5177\u4f53\u903b\u8f91\u53ef\u4ee5\u67e5\u770b\u4e0a\u65b9\u8d34\u51fa\u6765\u7684\u6e90\u7801\u3002\u4e0b\u9762\u662f\u6280\u672f\u8981\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6267\u884c\u5230 <code>core.OnCompleted<\/code> \uff0c\u68c0\u67e5\u662f\u5426\u4fdd\u5b58\u8fc7\u56de\u8c03\u65b9\u6cd5\uff1a\n<ol class=\"wp-block-list\">\n<li>\u82e5\u6ca1\u6709\u4fdd\u5b58\uff0c\u5373\u7b2c\u4e00\u6b21\u8c03\u7528 <code>OnCompleted<\/code> \uff0c\u5219\u5c06\u56de\u8c03\u65b9\u6cd5\u4fdd\u5b58\u4e0b\u6765\uff0c\u4f9b <code>TrySet***<\/code> \u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u82e5\u5df2\u7ecf\u4fdd\u5b58\u8fc7\uff0c\u5219\u68c0\u67e5\u56de\u8c03\u662f\u5426\u4e0e <code>STaskCompletionSourceCoreShared.s_sentinel<\/code> \u76f8\u7b49\uff0c\u82e5\u4e0d\u76f8\u7b49\uff0c\u8868\u660e\u51fa\u73b0\u4e86\u591a\u6b21\u7b49\u5f85\u7684\u60c5\u51b5\uff0c\u5373\u591a\u6b21\u8c03\u7528\u4e86 <code>OnCompleted<\/code> \uff0c\u9700\u8981\u629b\u51fa\u5f02\u5e38\uff1b\u82e5\u76f8\u7b49\uff0c\u8868\u660e\u5148\u6267\u884c\u4e86 <code>TrySet***<\/code> \uff0c\u56e0\u6b64\u76f4\u63a5\u6267\u884c\u56de\u8c03\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u6267\u884c\u5230 <code>core.TrySet***<\/code> \uff0c\u68c0\u67e5\u662f\u5426\u4fdd\u5b58\u8fc7\u56de\u8c03\u65b9\u6cd5\uff1a\n<ol class=\"wp-block-list\">\n<li>\u82e5\u4fdd\u5b58\u8fc7\uff0c\u8868\u660e <code>OnCompleted<\/code> \u5df2\u7ecf\u6267\u884c\uff0c\u8fd9\u91cc\u76f4\u63a5\u6267\u884c\u56de\u8c03\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u82e5\u6ca1\u6709\u4fdd\u5b58\u8fc7\uff0c\u8868\u660e\u5148\u6267\u884c\u4e86 <code>TrySet***<\/code> \uff0c\u6211\u4eec\u9700\u8981\u5c06\u56de\u8c03\u8bbe\u7f6e\u6210 <code>s_sentinel<\/code> \uff0c\u907f\u514d\u591a\u6b21\u7b49\u5f85 STask\u3002<\/li>\n\n\n\n<li>\u628a\u56de\u8c03\u8bbe\u7f6e\u6210 <code>s_sentinel<\/code> \u8fd9\u4e00\u6b65\u7684\u5904\u7406\u6bd4\u8f83\u5de7\u5999\uff0c\u5229\u7528\u4e86C#\u6761\u4ef6\u6216\u8bed\u53e5\u7684\u7279\u6027\uff0c\u5927\u4f19\u53ef\u4ee5\u81ea\u884c\u611f\u53d7\u4e0b\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u7531\u4e8e <code>STaskCompletionSourceCoreShared.s_sentinel<\/code> \u53ea\u88ab\u7528\u4e8e\u8ddf\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\uff0c\u5176\u672c\u8eab\u4e0d\u4f1a\u6709\u53d8\u5316\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u5b9a\u4e49\u4e3a\u9759\u6001\u7c7b\u578b\uff0c\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">STaskScheduler<\/h2>\n\n\n\n<p>\u5728 <code>STaskCompletionSourceCore<\/code> \u548c <code>ExceptionHolder<\/code> \u4e2d\uff0c\u6211\u4eec\u7528\u5230\u4e86 <code>STaskScheduler<\/code> \u6765\u5904\u7406\u5f02\u5e38\u2014\u2014\u628a\u5f02\u5e38\u5bf9\u8c61\u53d1\u9001\u51fa\u53bb\uff0c\u81f3\u4e8e\u53d1\u51fa\u53bb\u4e4b\u540e\u5177\u4f53\u600e\u4e48\u5904\u7406\uff0c\u5219\u4ea4\u7ed9 <code>STaskScheduler<\/code> \u8d1f\u8d23\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\uff0c.NET Framework \u5c31\u63d0\u4f9b\u4e86 <code>TaskScheduler<\/code> \u7ed9\u7528\u6237\u4f7f\u7528\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u63a5\u53d7\u5916\u90e8\u4f20\u6765\u7684\u4efb\u52a1\uff0c\u5c06\u8fd9\u4e9b\u4efb\u52a1\u6d3e\u53d1\u5230\u76f8\u5173\u7684\u7ebf\u7a0b\u4e0a\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u4e14\u5b83\u80fd\u591f\u4fdd\u8bc1\u8fd9\u4e9b\u4efb\u52a1\u6700\u7ec8\u80fd\u591f\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u7684 <code>STaskScheduler<\/code> \u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u6211\u4eec\u53ea\u9700\u8981\u5b83\u5904\u7406\u5f02\u5e38\uff0c\u56e0\u6b64\u5bf9\u5b83\u505a\u4e86\u4e00\u4e9b\u201c\u7b80\u5316\u201d\u3002\u5b9e\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/\/ &lt;summary>\r\n    \/\/\/ STask \u6ca1\u6709\u7c7b\u4f3c TaskScheduler \u7684 scheduler\uff0c\u8be5\u7c7b\u53ea\u5904\u7406\u672a\u5904\u7406\u7684\u5f02\u5e38\r\n    \/\/\/ &lt;\/summary>\r\n    public static class STaskScheduler\r\n    {\r\n        public static event Action&lt;Exception> UnobservedTaskException;\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u662f\u5426\u8c03\u7528 UnobservedTaskException \u6765\u5904\u7406 OperationCanceledException\uff1b\u9ed8\u8ba4\u4e3a false\r\n        \/\/\/ &lt;\/summary>\r\n        public static bool PropagateOperationCanceledException = false;\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u6355\u83b7\u5230\u672a\u5904\u7406\u5f02\u5e38\u4e14\u6ca1\u7ed1\u5b9a UnobservedTaskException \u65f6\uff0c\u5199\u5230 UnityLog \u7684 LogType\uff1b\u9ed8\u8ba4\u4e3aException\r\n        \/\/\/ &lt;\/summary>\r\n        public static UnityEngine.LogType UnobservedExceptionWriteLogType = UnityEngine.LogType.Exception;\r\n\r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u662f\u5426\u5c06\u5f02\u5e38\u53d1\u56de Unity \u4e3b\u7ebf\u7a0b\uff1b\u9ed8\u8ba4\u4e3a true\r\n        \/\/\/ &lt;\/summary>\r\n        public static bool DispatchUnityMainThread = true;\r\n\r\n        private static void InvokeUnobservedTaskException(object state)\r\n        {\r\n            UnobservedTaskException((Exception)state);\r\n        }\r\n        \r\n        \/\/\/ &lt;summary>\r\n        \/\/\/ \u59d4\u6258\u7f13\u5b58\r\n        \/\/\/ &lt;\/summary>\r\n        private static readonly SendOrPostCallback handleExceptionInvoke = InvokeUnobservedTaskException;\r\n\r\n        internal static void PublishUnobservedTaskException(Exception ex)\r\n        {\r\n            if (ex != null)\r\n            {\r\n                if (!PropagateOperationCanceledException &amp;&amp; ex is OperationCanceledException)\r\n                {\r\n                    return;\r\n                }\r\n\r\n                if (UnobservedTaskException != null)\r\n                {\r\n                    \/\/\u4f7f\u7528\u5916\u90e8\u63d0\u4f9b\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u9700\u8981\u8003\u8651\u4e3b\u7ebf\u7a0b\u4e0e\u5b50\u7ebf\u7a0b\u7684\u95ee\u9898\r\n                    if (!DispatchUnityMainThread || Thread.CurrentThread.ManagedThreadId == MainThreadId)\r\n                    {\r\n                        UnobservedTaskException.Invoke(ex);\r\n                    }\r\n                    else\r\n                    {\r\n                        UnitySynchronizationContext.Post(handleExceptionInvoke, ex);\r\n                    }\r\n                }\r\n                else\r\n                {\r\n                    string msg = null;\r\n                    if (UnobservedExceptionWriteLogType != UnityEngine.LogType.Exception)\r\n                    {\r\n                        msg = \"UnobservedTaskException: \" + ex.ToString();\r\n                    }\r\n                    switch (UnobservedExceptionWriteLogType)\r\n                    {\r\n                        case UnityEngine.LogType.Error:\r\n                            UnityEngine.Debug.LogError(msg);\r\n                            break;\r\n                        case UnityEngine.LogType.Assert:\r\n                            UnityEngine.Debug.LogAssertion(msg);\r\n                            break;\r\n                        case UnityEngine.LogType.Warning:\r\n                            UnityEngine.Debug.LogWarning(msg);\r\n                            break;\r\n                        case UnityEngine.LogType.Log:\r\n                            UnityEngine.Debug.Log(msg);\r\n                            break;\r\n                        case UnityEngine.LogType.Exception:\r\n                            UnityEngine.Debug.LogException(ex);\r\n                            break;\r\n                        default:\r\n                            break;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        \r\n        public static int MainThreadId { get; private set; } \r\n        public static SynchronizationContext UnitySynchronizationContext { get; private set; }\r\n        \r\n        &#91;RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]\r\n        private static void Init()\r\n        {\r\n            MainThreadId = Thread.CurrentThread.ManagedThreadId;\r\n            UnitySynchronizationContext = SynchronizationContext.Current;\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u91cd\u8981\u7684\u90e8\u5206\u6211\u5df2\u7ecf\u505a\u4e86\u4e9b\u6ce8\u91ca\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u90e8\u5206\u5c31\u662f\u4e3b\u7ebf\u7a0b\u4e0e\u5b50\u7ebf\u7a0b\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>STaskScheduler<\/code> \u662f\u5229\u7528 <code>UnityEngine.Debug.LogException<\/code> \u63a5\u53e3\u5c06\u5f02\u5e38\u5bf9\u8c61\u629b\u51fa\u7ed9\u7528\u6237\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u81ea\u5b9a\u4e49\u5904\u7406\u63a5\u53e3 <code>UnobservedTaskException<\/code> \u3002<\/p>\n\n\n\n<p>\u5f53\u4f7f\u7528\u81ea\u5b9a\u4e49\u5904\u7406\u63a5\u53e3\u65f6\uff0c\u6211\u4eec\u5c31\u9700\u8981\u8003\u8651\u201c\u5f53\u524d\u662f\u5426\u5728\u4e3b\u7ebf\u7a0b\u201d\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff0c\u7528\u6237\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u8c03\u7528\u4e86 <code>STaskScheduler<\/code> \uff0c\u4f46\u81ea\u5b9a\u4e49\u63a5\u53e3\u7ed1\u5b9a\u7684\u662f\u9700\u8981\u8fd0\u884c\u5728 Unity \u4e3b\u7ebf\u7a0b\u91cc\u7684\u65b9\u6cd5\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u5c06\u59d4\u6258\u53d1\u9001\u5230\u4e3b\u7ebf\u7a0b\u8fd9\u4e00\u6b65\u9aa4\uff0c\u4f7f\u7528\u4e86 <code>SynchronizationContext.Post<\/code> \u63a5\u53e3\u3002\u6211\u4eec\u5728 Unity \u521d\u59cb\u5316\u65f6\u5c06\u4e3b\u7ebf\u7a0b\u540c\u6b65\u4e0a\u4e0b\u6587\u7684 ID \u548c\u5b9e\u4f8b\u8bb0\u5f55\u4e0b\u6765\uff08\u6b64\u65f6\u5fc5\u5b9a\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff09\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u5b83\u5c31\u597d\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\u55f7\u3002<\/p>\n\n\n\n<p>\u5728 <code>UniTask\/STask<\/code> \u5e93\u91cc\uff0c\u8ddf\u4e3b\u7ebf\u7a0b\u76f8\u5173\u7684\u8bbe\u7f6e\u90fd\u5728 <code>PlayerLoopHelper<\/code> \u7c7b\u4e2d\uff0c\u8fd9\u91cc\u4e3a\u4e86\u65b9\u4fbf\u5c55\u793a\uff0c\u6211\u6682\u65f6\u5c06\u5b83\u5206\u79bb\u4e86\u51fa\u6765\u3002<\/p>\n\n\n\n<p><code>STaskScheduler<\/code> \u5e26\u7ed9\u6211\u4eec\u7684\u542f\u793a\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u5728\u6211\u4eec\u9047\u5230\u9700\u8981\u5c06\u4e00\u4e9b\u4ee3\u7801\u6d3e\u53d1\u5230\u4e3b\u7ebf\u7a0b\u6267\u884c\u7684\u60c5\u51b5\u65f6\uff0c\u4e5f\u53ef\u4ee5\u5957\u7528\u8fd9\u4e00\u5957\u7ec4\u5408\u62f3\u3002\u4e3e\u4e2a\u7b14\u8005\u7684\u4eb2\u8eab\u4f8b\u5b50\uff0c\u5f53\u4f7f\u7528 <code>ISerializationCallbackReceiver<\/code> \u63a5\u53e3\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5728\u63a5\u53e3\u65b9\u6cd5 <code>OnBeforeSerialize<\/code> \u6216 <code>OnAfterDeserialize<\/code> \u4e2d\u76f4\u63a5\u8c03\u7528 Unity \u63a5\u53e3\u662f\u4e0d\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u5728\u975e\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u56e0\u6b64\u53ea\u80fd\u91c7\u7528\u5c06\u4ee3\u7801\u6d3e\u53d1\u5230\u4e3b\u7ebf\u7a0b\u6267\u884c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STask\/Awaiter<\/h2>\n\n\n\n<p>\u4ecb\u7ecd\u4e86\u6700\u91cd\u8981\u7684 <code>STaskCompletionCore<\/code> \uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u8ba8\u8bba\u5904\u4e8e\u603b\u89c8\u56fe\u53e6\u4e00\u8fb9\u7684 <code>STask<\/code> \u4ee5\u53ca\u548c\u5b83\u914d\u5bf9\u7684 <code>Awaiter<\/code> \u3002<\/p>\n\n\n\n<p>\u4e0a\u4ee3\u7801\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public readonly struct STask&lt;T>\r\n    {\r\n        private readonly ISTaskSource&lt;T> source;\r\n        private readonly T result;\r\n        private readonly short token;\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public STask(T result)\r\n        {\r\n            this.source = default;\r\n            this.token = default;\r\n            this.result = result;\r\n        }\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public STask(ISTaskSource&lt;T> source, short token)\r\n        {\r\n            this.source = source;\r\n            this.token = token;\r\n            this.result = default;\r\n        }\r\n\r\n        public STaskStatus Status\r\n        {\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            get\r\n            {\r\n                return (this.source == null) ? STaskStatus.Succeeded : this.source.GetStatus(this.token);\r\n            }\r\n        }\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public Awaiter GetAwaiter()\r\n        {\r\n            return new Awaiter(this);\r\n        }\r\n\r\n        public override string ToString()\r\n        {\r\n            return (this.source == null) ? this.result?.ToString()\r\n                : \"(\" + this.source.UnsafeGetStatus() + \")\";\r\n        }\r\n\r\n        public readonly struct Awaiter : ICriticalNotifyCompletion\r\n        {\r\n            private readonly STask&lt;T> task;\r\n\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            public Awaiter(in STask&lt;T> task)\r\n            {\r\n                this.task = task;\r\n            }\r\n\r\n            public bool IsCompleted\r\n            {\r\n                &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n                get\r\n                {\r\n                    return this.task.Status.IsCompleted();\r\n                }\r\n            }\r\n\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            public T GetResult()\r\n            {\r\n                ISTaskSource&lt;T> source = this.task.source;\r\n                if (source == null)\r\n                {\r\n                    return this.task.result;\r\n                }\r\n                else\r\n                {\r\n                    return source.GetResult(this.task.token);\r\n                }\r\n            }\r\n\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            public void OnCompleted(Action continuation)\r\n            {\r\n                ISTaskSource&lt;T> source = this.task.source;\r\n                if (source == null)\r\n                {\r\n                    continuation();\r\n                }\r\n                else\r\n                {\r\n                    source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, this.task.token);\r\n                }\r\n            }\r\n\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            public void UnsafeOnCompleted(Action continuation)\r\n            {\r\n                ISTaskSource&lt;T> source = this.task.source;\r\n                if (source == null)\r\n                {\r\n                    continuation();\r\n                }\r\n                else\r\n                {\r\n                    source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, this.task.token);\r\n                }\r\n            }\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5148\u6765\u8ba8\u8bba <code>STask&lt;T&gt;<\/code> \u7684\u6210\u5458\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>source<\/code> \uff1a\u63a5\u53e3 <code>ISTaskSource&lt;T&gt;<\/code> \u5b9e\u4f8b\uff0c\u5b83\u662f\u5b9e\u73b0\u5f02\u6b65\u673a\u5236\u7684\u5bf9\u8c61\u3002\u5728\u8fd4\u56de\u503c\u4e3a <code>STask\/STask&lt;T&gt;<\/code> \u7684\u5f02\u6b65\u65b9\u6cd5\u7684\u5f02\u6b65\u673a\u5236\u4e2d\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u662f <code>AsyncSTask<\/code> \uff0c\u8fd9\u91cc\u7528\u63a5\u53e3\u6765\u58f0\u660e\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u540e\u671f\u7684\u6269\u5c55\u6027\u3002<\/li>\n\n\n\n<li><code>result<\/code> \uff1a\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u5b8c\u4e4b\u540e\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>token<\/code> \uff1a\u65f6\u6548\u4ee4\u724c\uff0c\u5728\u8fd9\u91cc\u7528\u6765\u7ed9 <code>STaskCompletionSourceCore<\/code> \u9a8c\u8bc1\uff0c\u5373\u4e0e\u5b83\u7684 <code>version<\/code> \u76f8\u6bd4\u3002\u5728\u521b\u5efa <code>STask<\/code> \u65f6\uff0c\u6211\u4eec\u4e5f\u4f1a\u521b\u5efa <code>core<\/code> \uff0c\u5e76\u5c06 <code>core.Version<\/code> \u544a\u8bc9\u7ed9 <code>STask<\/code> \uff0c\u5230 <code>STask<\/code> \u8fd9\u91cc\u5c31\u662f <code>token<\/code> \u3002\u7ed9\u540c\u4e00\u4e2a\u53d8\u91cf\u8d77\u4e24\u4e2a\u540d\u5b57\uff0c\u5b8c\u5168\u662f\u4f7f\u7528\u573a\u666f\u4e0d\u540c\u7684\u7f18\u6545\u3002<\/li>\n\n\n\n<li><code>Status<\/code> \uff1a\u5f53\u524d <code>STask<\/code> \u5f53\u524d\u7684\u6267\u884c\u72b6\u6001\u3002<\/li>\n\n\n\n<li><code>STask(ISTaskSource&lt;T&gt; source, short token)<\/code> \uff1a\u6784\u9020\u65b9\u6cd5\uff0c\u4e00\u822c\u6765\u8bf4\u6211\u4eec\u4f1a\u8c03\u7528\u5b83\u6765\u521b\u5efa STask\u3001Awaiter\u3002<\/li>\n\n\n\n<li><code>STask(T result)<\/code> \uff1a\u6784\u9020\u65b9\u6cd5\uff0c\u4f46\u53c2\u6570\u662f\u5f02\u6b65\u65b9\u6cd5\u7684\u7ed3\u679c\u3002\u6709\u65f6\u5728\u521b\u5efa STask \u4e4b\u524d\uff0c\u6211\u4eec\u4f1a\u5224\u65ad\u4efb\u52a1\u662f\u5426\u88ab\u53d6\u6d88\uff08\u6216\u8005\u51fa\u73b0\u5f02\u5e38\uff09\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u9ed8\u8ba4\u7ed3\u679c\u544a\u77e5\u7ed9 STask\uff1b\u6216\u8005\u6211\u4eec\u9700\u8981\u6784\u5efa\u5df2\u5b8c\u6210\u4efb\u52a1\u7684 STask \u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8be5\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>GetAwaiter()<\/code> \uff1a\u6ca1\u6709\u5b83\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u4f7f\u7528 await \u8bed\u53e5\u7b49\u5f85 STask \uff0c\u5f53\u7136\u5176\u8fd4\u56de\u7684 Awaiter \u9700\u8981\u6709 <code>IsCompleted<\/code> \u3001 <code>GetResult()<\/code> \u3001 <code>OnCompleted()\/UnsafeOnCompleted()<\/code> \u3002<\/li>\n<\/ul>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u8ba8\u8bba <code>Awaiter<\/code> \u7684\u6210\u5458\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>task<\/code> \uff1a\u521b\u5efa\u8be5 Awaiter \u7684 STask\uff0c\u6211\u4eec\u8981\u901a\u8fc7\u5b83\u6765\u83b7\u53d6\u5f02\u6b65\u903b\u8f91\u9700\u8981\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><code>IsCompleted<\/code> \uff1a\u8fd4\u56de\u5f53\u524d\u5f02\u6b65\u4efb\u52a1\u7684\u6267\u884c\u72b6\u6001\uff0c\u4efb\u52a1\u521a\u5f00\u59cb\u65f6\uff0c\u72b6\u6001\u673a\u4f1a\u8bbf\u95ee\u5b83\u6765\u5224\u65ad\u4efb\u52a1\u662f\u5426\u5b8c\u6210\uff0c\u82e5\u5df2\u7ecf\u5b8c\u6210\u5219\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n\n\n\n<li><code>Awaiter(STask&lt;T&gt; task)<\/code> \uff1a\u5e73\u5e73\u65e0\u5947\u7684\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>GetResult()<\/code> \uff1a\u5f02\u6b65\u4efb\u52a1\u7ed3\u675f\u540e\uff0c\u72b6\u6001\u673a\u4f1a\u8c03\u7528\u5b83\u6765\u83b7\u53d6\u7ed3\u679c\uff0c\u5e76\u8fd4\u56de\u3002<\/li>\n\n\n\n<li><code>OnCompleted()\/UnsafeOnCompleted()<\/code> \uff1a\u5f53\u5f02\u6b65\u4efb\u52a1\u9700\u8981\u7b49\u5f85\uff08\u5373\u4e0d\u662f\u4ee5\u6765\u5c31\u5df2\u5b8c\u6210\uff09\uff0c\u72b6\u6001\u673a\u4f1a\u8c03\u7528\u5b83\u6765\u6ce8\u518c\u56de\u8c03\u3002<\/li>\n<\/ul>\n\n\n\n<p>Awaiter \u7684\u65b9\u6cd5 <code>GetResult()<\/code> \u3001 <code>OnCompleted()\/UnsafeOnCompleted()<\/code> \u4e2d\u6211\u4eec\u4f1a\u5224\u65ad <code>task.source<\/code> \u662f\u5426\u4e3a\u7a7a\uff1a\u82e5\u4e3a\u7a7a\uff0c\u5219\u8868\u660e\u662f\u901a\u8fc7 <code>STask(T result)<\/code> \u521b\u5efa\u7684\uff0c\u90a3\u83b7\u53d6\u7ed3\u679c\u7684\u5c31\u76f4\u63a5\u8fd4\u56de\uff0c\u6ce8\u518c\u56de\u8c03\u7684\u5c31\u76f4\u63a5\u8c03\u7528\u56de\u8c03\uff08\u56e0\u4e3a\u5f02\u6b65\u4efb\u52a1\u5df2\u7ecf\u7ed3\u675f\u4e86\uff09\uff1b\u82e5\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8868\u660e\u662f\u901a\u8fc7 <code>STask(ISTaskSource&lt;T&gt; source, short token)<\/code> \u521b\u5efa\u7684\uff0c\u6211\u4eec\u5c31\u9700\u8981\u7528 <code>task.source<\/code> \u6765\u5904\u7406\u76f8\u5173\u903b\u8f91\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">AsyncSTask<\/h2>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u6765\u8ba8\u8bba <code>AsyncSTask&lt;TStateMachine, T&gt;<\/code> \uff0c\u6211\u4eec\u8fd9\u91cc\u4e3e\u4f8b\u7684\u662f\u6709\u8fd4\u56de\u503c\u7248\u672c\uff0c\u540e\u9762\u6211\u4eec\u5c06\u5b83\u7b80\u79f0\u4e3a <code>AsyncSTask<\/code> \u3002<\/p>\n\n\n\n<p><code>AsyncSTask<\/code> \u5b9e\u73b0\u4e86\u4e00\u5171\u4e09\u4e2a\u63a5\u53e3\uff0c\u5176\u4e2d <code>ITaskPoolNode&lt;AsyncSTask&gt;<\/code> \u63a5\u53e3\u8868\u793a\u5b83\u5c06\u81ea\u5df1\u201c\u6c60\u5316\u201d\uff0c\u53d8\u6210\u53ef\u4ee5\u653e\u8fdb\u5bf9\u8c61\u6c60\u91cc\u7684\u7ed3\u6784\u2014\u2014\u6ca1\u9519\uff0c\u6211\u4eec\u9700\u8981\u7f13\u5b58 <code>AsyncSTask<\/code> \u3002\u7b2c\u4e8c\u4e2a\u63a5\u53e3 <code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \uff0c\u662f\u5728\u72b6\u6001\u673a <code>AsyncSTaskMethodBuilder&lt;T&gt;<\/code> \u4e2d\u88ab\u5f15\u7528\u7684\uff0c\u7b80\u800c\u8a00\u4e4b\uff0c\u72b6\u6001\u673a\u901a\u8fc7\u8be5\u63a5\u53e3\u8fd9\u79cd\u201c\u62bd\u8c61\u5bf9\u8c61\u201d\u4e0e <code>AsyncSTask<\/code> \u5efa\u7acb\u4e86\u8fde\u63a5\u3002\u6709\u5f02\u66f2\u540c\u5de5\u4e4b\u5999\u7684\u7b2c\u4e09\u4e2a\u63a5\u53e3 <code>ISTaskSource&lt;T&gt;<\/code> \uff0c\u5728 <code>STask&lt;T&gt;<\/code> \u4e2d\u88ab\u5f15\u7528\uff0c\u5177\u4f53\u7528\u6cd5\u53ef\u4ee5\u53c2\u7167\u4e0a\u4e00\u5c0f\u8282\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u6700\u9700\u8981\u52a0\u6df1\u7406\u89e3\u7684\u662f <code>ISTaskSource&lt;T&gt;<\/code> \u63a5\u53e3\uff0c\u5176\u6b21\u662f <code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u8bb2\u8bb2\u7b80\u5355\u540e\u8005 <code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \uff1aC#\u7684\u63a5\u53e3\uff0c\u662f\u975e\u5e38\u5f3a\u5927\u7684\u89e3\u8026\u5de5\u5177\uff0c\u4f7f\u7528\u8005\u53ea\u9700\u8981\u5173\u5fc3\u63a5\u53e3\u5e2e\u6211\u4eec\u62bd\u8c61\u51fa\u6765\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b8c\u5168\u4e0d\u5173\u5fc3\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u5e76\u4e14\u767e\u5206\u767e\u76f8\u4fe1\u63a5\u53e3\u7684\u5b9e\u73b0\u8005\u4e0d\u4f1a\u51fa\u95ee\u9898\u3002\u65e2\u7136\u6211\u53ea\u5173\u5fc3\u63a5\u53e3\u7684\u65b9\u6cd5\uff0c\u90a3\u4e48\u5177\u4f53\u662f\u8c01\u6765\u5b9e\u73b0\u8ddf\u6211\u4e5f\u6beb\u65e0\u5173\u7cfb\uff08\u8fd9\u79cd\u8bf4\u6cd5\u6bd4\u8f83\u7edd\u5bf9\uff0c\u56e0\u4e3a\u6709\u65f6\u5019\u4f7f\u7528\u8005\u4e5f\u662f\u77e5\u9053\u63a5\u53e3\u7684\u5177\u4f53\u5b9e\u73b0\u8005\u7684\uff0c\u4f46\u8ba9\u5b83\u4e0d\u77e5\u9053\u662f\u6211\u4eec\u4f7f\u7528\u63a5\u53e3\u7684\u76ee\u7684\uff09\u3002<code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \u88ab\u72b6\u6001\u673a\u4f7f\u7528\uff0c\u72b6\u6001\u673a\u7528\u5b83\u6765\u5b9e\u73b0\u5177\u4f53\u7684\u903b\u8f91\uff0c\u5f53\u6211\u4eec\u60f3\u66f4\u6362 <code>AsyncSTask<\/code> \u8fd9\u4e2a\u6253\u5de5\u4ed4\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u81ea\u5df1\u5728\u5b9e\u73b0\u4e00\u6b21 <code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \u63a5\u53e3\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u662f\u63a5\u53e3 <code>ISTaskSource&lt;T><\/code> \uff1a\u4e4b\u6240\u4ee5\u8bf4\u5b83\u66f4\u91cd\u8981\uff0c\u662f\u56e0\u4e3a\u5728\u540e\u7eed\u6269\u5c55 <code>STask<\/code> \u5e93\u7684\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u514d\u4e0d\u4e86\u8ddf\u5b83\u6253\u4ea4\u9053\u3002\u4e0a\u9762\u63d0\u5230\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0 <code>IStateMachineRunnerPromise&lt;T><\/code> \uff0c\u66ff\u6362\u6389\u73b0\u5728\u7684 <code>AsyncSTask<\/code> \uff0c\u4f46\u6211\u4eec\u51e0\u4e4e\u4e0d\u4f1a\u8fd9\u6837\u505a\u2014\u2014\u56e0\u4e3a <code>AsyncSTask<\/code> \u5df2\u7ecf\u505a\u5f97\u975e\u5e38\u597d\u4e86\u3002\u90a3\u4e48 <code>ISTaskSource&lt;T><\/code> \u662f\u5982\u4f55\u5e2e\u52a9\u6211\u4eec\u6269\u5c55 <code>STask<\/code> \u5e93\u7684\u5462\uff1f\u6211\u4eec\u7ee7\u7eed\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public async STask&lt;float> MethodAsync()\r\n{\r\n    float thisTime = Time.realtimeSinceStartup;\r\n    await STask.Delay(5000);\/\/\u7b49\u5f855\u79d2\r\n    return Time.realtimeSinceStartup - thisTime;\r\n}\r\n\r\npublic void Start()\r\n{\r\n    float awaitTime = await MethodAsync();\r\n    Debug.Log($\"consume time: {awaitTime}s\");\r\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e00\u5171\u6709\u4e24\u5904\u4f7f\u7528\u4e86 await \u7684\uff0c\u6309\u7167\u4f7f\u7528\u987a\u5e8f\u6765\uff0c\u5206\u522b\u662f\u7b49\u5f85 <code>MethodAsync()<\/code> \u548c <code>STask.Delay()<\/code> \u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MethodAsync()<\/code> \u662f\u5f02\u6b65\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a STask \u5bf9\u8c61\uff0c\u5e76\u4e14\u8be5 STask \u5bf9\u8c61\u7684 ISTaskSource \u63a5\u53e3\u5b9e\u4f8b\u4e3a AsyncSTask\u3002AsyncSTask \u5c31\u4f1a\u5e2e\u52a9 STask \u6765\u5904\u7406\u5f02\u6b65\u76f8\u5173\u7684\u903b\u8f91\u3002<\/li>\n\n\n\n<li><code>STask.Delay<\/code> \u4e0d\u662f\u5f02\u6b65\u65b9\u6cd5\uff0c\u4f46\u5b83\u4ecd\u7136\u4f1a\u8fd4\u56de\u4e00\u4e2a STask \u5bf9\u8c61\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u7528 await \u53bb\u7b49\u5f85\u5b83\u3002\u5b83\u8fd4\u56de\u7684 STask \u5bf9\u8c61\u7684 ISTaskSource \u63a5\u53e3\u5b9e\u4f8b\u662f\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\u7684 <code>DelayPromise<\/code> \uff0c\u5b83\u4f1a\u5e2e\u6211\u4eec\u8ba1\u65f6\uff0c\u5f53\u5230\u4e86\u8bbe\u5b9a\u7684\u65f6\u95f4\u540e\uff0c\u5b83\u4f1a\u901a\u77e5 <code>STaskCompletionSourceCore<\/code> \uff08<code>DelayPromise<\/code> \u5185\u90e8\u4ecd\u7136\u4f9d\u8d56 <code>STaskCompletionSourceCore<\/code> \uff0c\u901a\u77e5\u5373\u8c03\u7528 <code>core.TrySetResult<\/code> \uff09\uff0c\u8fd9\u65f6\u7b49\u5f85\u4efb\u52a1\u7ed3\u675f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6709\u673a\u4f1a\u7684\u8bdd\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u5355\u72ec\u8ba8\u8bba STask \u5e93\u7684\u6269\u5c55\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f <code>AsyncSTask<\/code> \u6e90\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>internal sealed class AsyncSTask&lt;TStateMachine, T> : IStateMachineRunnerPromise&lt;T>, ISTaskSource&lt;T>, ITaskPoolNode&lt;AsyncSTask&lt;TStateMachine, T>>\r\n        where TStateMachine : IAsyncStateMachine\r\n    {\r\n        private TStateMachine stateMachine;\r\n        private STaskCompletionSourceCore&lt;T> core;\r\n\r\n        private AsyncSTask()\r\n        {\r\n            this.MoveNext = this.Run;\r\n        }\r\n\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        private void Run()\r\n        {\r\n            this.stateMachine.MoveNext();\r\n        }\r\n\r\n\r\n        #region Pool\r\n        private static TaskPool&lt;AsyncSTask&lt;TStateMachine, T>> pool;\r\n\r\n        public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise&lt;T> runnerPromiseFieldRef)\r\n        {\r\n            if (!pool.TryPop(out var result))\r\n            {\r\n                result = new AsyncSTask&lt;TStateMachine, T>();\r\n            }\r\n\r\n            runnerPromiseFieldRef = result; \/\/ set runner before copied.\r\n            result.stateMachine = stateMachine; \/\/ copy struct StateMachine(in release build).\r\n        }\r\n\r\n        private AsyncSTask&lt;TStateMachine, T> nextNode;\r\n        public ref AsyncSTask&lt;TStateMachine, T> NextNode => ref this.nextNode;\r\n\r\n        static AsyncSTask()\/\/\u9759\u6001\u6784\u9020\u51fd\u6570\uff0c\u53ea\u6267\u884c\u4e00\u6b21\uff08\u5b9e\u4f8b\u5316\u524d\u6216\u5f15\u7528\u5176\u4ed6\u9759\u6001\u6210\u5458\u524d\u8c03\u7528\u4e00\u6b21\uff09\r\n        {\r\n            TaskPool.RegisterSizeGetter(typeof(AsyncSTask&lt;TStateMachine, T>), () => pool.Size);\r\n        }\r\n        #endregion\r\n\r\n        private void Return()\r\n        {\r\n            this.core.Reset();\r\n            this.stateMachine = default;\r\n            pool.TryPush(this);\r\n        }\r\n\r\n        private bool TryReturn()\r\n        {\r\n            this.core.Reset();\r\n            this.stateMachine = default;\r\n            return pool.TryPush(this);\r\n        }\r\n\r\n        #region IStateMachineRunnerPromise&lt;T>\r\n        public Action MoveNext { get; }\r\n\r\n        public STask&lt;T> Task\r\n        {\r\n            get\r\n            {\r\n                return new STask&lt;T>(this, this.core.Version);\r\n            }\r\n        }\r\n\r\n        public void SetException(Exception exception)\r\n        {\r\n            this.core.TrySetException(exception);\r\n        }\r\n\r\n        public void SetResult(T result)\r\n        {\r\n            this.core.TrySetResult(result);\r\n        }\r\n        #endregion\r\n\r\n        #region ISTaskSource&lt;T>\r\n        public T GetResult(short token)\r\n        {\r\n            try\r\n            {\r\n                return this.core.GetResult(token);\r\n            }\r\n            finally\r\n            {\r\n                this.TryReturn();\r\n            }\r\n        }\r\n\r\n        void ISTaskSource.GetResult(short token)\r\n        {\r\n            this.GetResult(token);\r\n        }\r\n\r\n        public STaskStatus GetStatus(short token)\r\n        {\r\n            return this.core.GetStatus(token);\r\n        }\r\n\r\n        public STaskStatus UnsafeGetStatus()\r\n        {\r\n            return this.core.UnsafeGetStatus();\r\n        }\r\n\r\n        public void OnCompleted(Action&lt;object> continuation, object state, short token)\r\n        {\r\n            this.core.OnCompleted(continuation, state, token);\r\n        }\r\n        #endregion\r\n    }<\/code><\/pre>\n\n\n\n<p>\u5c0f\u7f16\u5df2\u7ecf\u5c06\u4ee3\u7801\u6309\u63a5\u53e3\u8fdb\u884c\u5206\u7c7b\u4e86\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u6309\u7167\u8fd9\u79cd\u5206\u7c7b\u6765\u5927\u81f4\u8ba8\u8bba\u4e0b <code>AsyncSTask<\/code> \u7684\u6210\u5458\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>IStateMachineRunnerPromise&lt;T&gt;<\/code> \uff1a\u5b9e\u73b0\u5b83\uff0c\u4f60\u5c31\u80fd\u6210\u4e3a\u72b6\u6001\u673a\u5927\u540d\u9f0e\u9f0e\u7684\u6253\u5de5\u4ed4\uff01\n<ul class=\"wp-block-list\">\n<li><code>Task<\/code> \uff1a\u72b6\u6001\u673a\u901a\u8fc7\u5b83\u83b7\u53d6\u53ef\u7b49\u5f85\u5bf9\u8c61\uff08\u5728\u8fd9\u91cc\u4e3a STask \u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li><code>SetException(Exception)<\/code> \uff1a\u6267\u884c\u4e2d\u9047\u5230\u5f02\u5e38\uff0c\u72b6\u6001\u673a\u8c03\u7528\u5b83\u6765\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>SetResult(T)<\/code> \uff1a\u6267\u884c\u5b8c\u6bd5\uff0c\u72b6\u6001\u673a\u8c03\u7528\u5b83\u8bbe\u7f6e\u7ed3\u679c\/\u8fd4\u56de\u503c\u3002<\/li>\n\n\n\n<li><code>Action MoveNext<\/code> \uff1a\u8be5\u63a5\u53e3\u4e0b\u6700\u91cd\u8981\u7684\u6210\u5458\uff0c<code>MoveNext<\/code> \u59d4\u6258\u7ed1\u5b9a\u72b6\u6001\u673a\u7684 <code>MonveNext()<\/code> \u65b9\u6cd5\uff08\u5177\u4f53\u53ef\u770b\u6784\u9020\u65b9\u6cd5 <code>AsyncSTask()<\/code> \u548c <code>Run()<\/code> \u65b9\u6cd5\uff09\uff0c\u8be5\u59d4\u6258\u4f1a\u88ab\u72b6\u6001\u673a\u6ce8\u518c\u7ed9 awaiter\uff0c\u5f53\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u8be5\u59d4\u6258\u4f1a\u88ab\u8c03\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>ISTaskSource&lt;T&gt;<\/code> \uff1a\u8ddf STask \u7ed3\u5408\u7684\u5bc6\u7801\uff0c\u5b9e\u73b0\u4e86\u5b83\uff0cSTask \u624d\u4f1a\u7b54\u5e94\u548c\u6211\u4eec\u4e00\u8d77\u73a9\u3002\n<ul class=\"wp-block-list\">\n<li><code>GetStatus(short token)<\/code> \uff1a\u83b7\u53d6\u5f53\u524d\u4efb\u52a1\u7684\u6267\u884c\u72b6\u6001\uff0c\u4e00\u822c\u7528\u4e8e\u5728\u7b49\u5f85\u5f00\u59cb\u524d\u5224\u65ad\u4efb\u52a1\u662f\u5426\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u907f\u514d\u518d\u6b21\u7b49\u5f85\u9020\u6210\u6027\u80fd\u6d6a\u8d39\u3002<\/li>\n\n\n\n<li><code>OnCompleted(Action&lt;object&gt; continuation, object state, short token)<\/code> \uff1a\u7528\u4e8e\u6ce8\u518c\u56de\u8c03\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><code>T GetResult(short token)<\/code> \uff1a\u4efb\u52a1\u7ed3\u675f\u540e\uff0c\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>ITaskPoolNode&lt;T&gt;<\/code> \uff1a\u5b9e\u73b0 <code>AsyncSTask<\/code> \u7684\u5bf9\u8c61\u6c60\u3002\n<ul class=\"wp-block-list\">\n<li><code>TaskPool&lt;T&gt; pool<\/code> \uff1a\u5bf9\u8c61\u6c60\u672c\u6c60\u3002<\/li>\n\n\n\n<li><code>SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise&lt;T&gt; runnerPromiseFieldRef)<\/code> \uff1a\u8be5\u65b9\u6cd5\u88ab\u72b6\u6001\u673a\u8c03\u7528\uff0c\u72b6\u6001\u673a\u901a\u8fc7\u5b83\u6765\u83b7\u53d6 <code>AsyncSTask<\/code> \u5bf9\u8c61\uff0c\u540c\u65f6\u628a\u81ea\u8eab\uff08\u72b6\u6001\u673a\u5bf9\u8c61\uff09\u4f20\u9012\u7ed9 <code>AsyncSTask<\/code> \u3002<\/li>\n\n\n\n<li><code>AsyncSTask&lt;T&gt; NextNode<\/code> \uff1a\u6307\u5411\u5bf9\u8c61\u6c60\u4e2d\u7684\u4e0b\u4e00\u4e2a <code>AsyncSTask<\/code> \u5bf9\u8c61\u3002\u5bf9\u8c61\u6c60\u6ca1\u6709\u5355\u72ec\u7684\u5bb9\u5668\u6765\u4fdd\u5b58\u6240\u6709\u6c60\u4e2d\u7684\u5bf9\u8c61\uff0c\u800c\u662f\u91c7\u7528\u94fe\u8868\u7684\u65b9\u5f0f\u6765\u7ec4\u7ec7\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u9664\u6b64\u4e4b\u5916\uff0c<code>AsyncSTask<\/code> \u8fd8\u62e5\u6709\u81ea\u8eab\u7684\u6210\u5458\uff1a\n<ul class=\"wp-block-list\">\n<li><code>Run()<\/code> \uff1a\u8be5\u65b9\u6cd5\u88ab\u7ed1\u5b9a\u5230 <code>MoveNext<\/code> \u59d4\u6258\uff0c\u5e76\u5728\u5185\u90e8\u8c03\u7528\u72b6\u6001\u673a\u7684 <code>MoveNext()<\/code> \u65b9\u6cd5\uff0c\u63a8\u52a8\u72b6\u6001\u673a\u8fd0\u884c\u3002\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u7591\u60d1\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u5c06 <code>MoveNext<\/code> \u59d4\u6258\u7ed1\u5b9a <code>MoveNext()<\/code> \u65b9\u6cd5\uff0c\u800c\u8981\u4e2d\u95f4\u518d\u52a0\u4e00\u5c42 <code>Run()<\/code> \uff0c\u7b14\u8005\u63a8\u6d4b\u8fd9\u662f\u4e3a\u4e86\u65b9\u4fbf\u5728\u8c03\u8bd5\u65f6\u67e5\u770b\u8c03\u7528\u5806\u6808\u6240\u505a\u7684\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><code>Return()<\/code> \uff1a\u5728 <code>GetResult<\/code> \u65b9\u6cd5\u4e2d\u8c03\u7528\uff0c\u5f53\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u6700\u7ec8\u9700\u8981\u91cd\u7f6e\u7528\u8fc7\u7684\u5bf9\u8c61\uff0c\u5e76\u5c06\u81ea\u8eab\u653e\u5165\u5bf9\u8c61\u6c60\u3002<\/li>\n\n\n\n<li><code>TryRetuen()<\/code> \uff1a<code>Return()<\/code> \u7684\u6709\u8fd4\u56de\u503c\u7248\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u4e00\u822c\u7528\u5b83\uff0c\u800c\u4e0d\u662f <code>Return()<\/code> \u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">AsyncSTaskMethodBuilder<\/h2>\n\n\n\n<p>\u7ec8\u4e8e\u8f6e\u5230\u5b83\u4e86\u2014\u2014<code>AsyncSTaskMethodBuilder<\/code> \u4f5c\u4e3a\u5f02\u6b65\u72b6\u6001\u673a\u7684\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u770b\u4f5c\u5f02\u6b65\u72b6\u6001\u673a\u5bf9\u5916\u63d0\u4f9b\u7684\u53ef\u81ea\u5b9a\u4e49\u63a5\u53e3\u3002\u548c <code>AsyncSTask<\/code> \u4e00\u6837\uff0c\u5b83\u4e5f\u6709\u5e26\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u6cdb\u578b\u53d8\u4f53\uff08\u6211\u4eec\u5728\u540e\u9762\u8ba8\u8bba\u7684\u4e5f\u662f\u8fd9\u4e2a\uff09\uff0c\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u4eec\u5c31\u76f4\u63a5\u79f0\u547c\u5b83\u4e3a <code>AsyncSTaskMethodBuilder<\/code> \u3001<code>builder<\/code> \u6216 \u201c\u72b6\u6001\u673a\u201d\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u6211\u4eec\u5728\u7b2c\u4e00\u7ae0\u91cc\u5df2\u7ecf\u8ba8\u8bba\u8fc7\u4e86 \u201cBuilder Type\u201d\uff0c\u77e5\u9053\u4e86\u81ea\u5b9a\u4e49\u72b6\u6001\u673a\u7684\u7279\u5f81\u548c\u9700\u8981\u9075\u5faa\u7684\u89c4\u5219\u3002STask \u4e2d\u7684 <code>builder<\/code> \u51e0\u4e4e\u6ca1\u6709\u53d8\u5316\uff0c\u56e0\u6b64\u6211\u4eec\u7684\u7814\u7a76\u91cd\u70b9\u843d\u5728\u4e86\u72b6\u6001\u673a\u6210\u5458\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t&#91;StructLayout(LayoutKind.Auto)]\r\n    public struct AsyncSTaskMethodBuilder&lt;T>\r\n    {\r\n        private IStateMachineRunnerPromise&lt;T> runnerPromise;\r\n        private Exception ex;\r\n        private T result;\r\n\r\n        \/\/ 1. Static Create method\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public static AsyncSTaskMethodBuilder&lt;T> Create()\r\n        {\r\n            return default;\r\n        }\r\n\r\n        \/\/ 2. TaskLike Task property\r\n        public STask&lt;T> Task\r\n        {\r\n            &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n            get\r\n            {\r\n                if (this.runnerPromise != null)\r\n                {\r\n                    return this.runnerPromise.Task;\r\n                }\r\n                else if (this.ex != null)\r\n                {\r\n                    return STask.FromException&lt;T>(this.ex);\r\n                }\r\n                else\r\n                {\r\n                    return STask.FromResult(this.result);\r\n                }\r\n            }\r\n        }\r\n\r\n        \/\/ 3. SetException\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void SetException(Exception exception)\r\n        {\r\n            if (this.runnerPromise == null)\r\n            {\r\n                this.ex = exception;\r\n            }\r\n            else\r\n            {\r\n                this.runnerPromise.SetException(exception);\r\n            }\r\n        }\r\n\r\n        \/\/ 4. SetResult\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void SetResult(T result)\r\n        {\r\n            if(this.runnerPromise == null)\r\n            {\r\n                this.result = result;\r\n            }\r\n            else\r\n            {\r\n                this.runnerPromise.SetResult(result);\r\n            }\r\n        }\r\n\r\n        \/\/ 5. AwaitOnCompleted\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void AwaitOnCompleted&lt;TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)\r\n            where TAwaiter : INotifyCompletion\r\n            where TStateMachine : IAsyncStateMachine\r\n        {\r\n            if (this.runnerPromise == null)\r\n            {\r\n                AsyncSTask&lt;TStateMachine, T>.SetStateMachine(ref stateMachine, ref this.runnerPromise);\r\n            }\r\n\r\n            awaiter.OnCompleted(this.runnerPromise.MoveNext);\r\n        }\r\n\r\n        \/\/ 6. AwaitUnsafeOnCompleted\r\n        &#91;SecuritySafeCritical]\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void AwaitUnsafeOnCompleted&lt;TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)\r\n            where TAwaiter : ICriticalNotifyCompletion\r\n            where TStateMachine : IAsyncStateMachine\r\n        {\r\n            if (this.runnerPromise == null)\r\n            {\r\n                AsyncSTask&lt;TStateMachine, T>.SetStateMachine(ref stateMachine, ref this.runnerPromise);\r\n            }\r\n\r\n            awaiter.UnsafeOnCompleted(this.runnerPromise.MoveNext);\r\n        }\r\n\r\n        \/\/ 7. Start\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void Start&lt;TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine\r\n        {\r\n            stateMachine.MoveNext();\r\n        }\r\n\r\n        \/\/ 8. SetStateMachine\r\n        &#91;MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n        public void SetStateMachine(IAsyncStateMachine stateMachine)\r\n        {\r\n            \/\/\u7531\u4e8e\u6027\u80fd\u95ee\u9898\uff0c\u8fd9\u91cc\u62d2\u7edd\u4f7f\u7528\u88c5\u7bb1\u4e4b\u540e\u7684\u72b6\u6001\u673a\uff0c\u6240\u4ee5 nothing happened\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u5206\u522b\u8ba8\u8bba\u5176\u4e2d\u7684\u6210\u5458\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>runnerPromise<\/code> \uff1a\u6301\u6709 <code>IStateMachineRunnerPromise<\/code> \u52b3\u52a8\u5408\u540c\u7684\u6253\u5de5\u4ed4\uff0c\u662f\u72b6\u6001\u673a\u4e2d\u6700\u91cd\u8981\u7684\u6210\u5458\uff0c\u5b83\u5e2e\u52a9\u72b6\u6001\u673a\u5b9e\u73b0\u5f02\u6b65\u903b\u8f91\u3002<\/li>\n\n\n\n<li><code>ex<\/code> \uff1a\u4efb\u52a1\u51fa\u73b0\u5f02\u5e38\u65f6\u7684\u5f02\u5e38\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>result<\/code> \uff1a\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><code>Create()<\/code> \uff1a\u9759\u6001 Create \u65b9\u6cd5\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u72b6\u6001\u673a\u672c\u8eab\u3002\u5728\u4ee3\u7801\u6267\u884c\u5230 await \u8bed\u53e5\u7b49\u5f85\u5f02\u6b65\u65b9\u6cd5\u65f6\u8c03\u7528\uff08\u8fd9\u91cc\u7684\u5f02\u6b65\u65b9\u6cd5\u6307\u6709 async \u5173\u952e\u5b57\uff0c\u8fd4\u56de\u7c7b\u578b\u4e3a task-like\uff0c\u5e76\u4e14\u8be5\u7c7b\u578b\u62e5\u6709\u53c2\u6570\u4e3a\u81ea\u5b9a\u4e49 builder \u7684 <code>AsyncMethodBuilder<\/code> \u7279\u6027\u7684\u65b9\u6cd5\uff09\u3002<\/li>\n\n\n\n<li><code>STask&lt;T&gt; Task<\/code> \uff1atask-like \u5bf9\u8c61\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u6211\u4eec\u81ea\u5df1\u7684 STask\u3002\u5728\u83b7\u53d6\u5b83\u65f6\uff0c\u6211\u4eec\u505a\u4e86\u51e0\u4e2a\u5224\u65ad\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5f53 <code>runnerPromise<\/code> \u5bf9\u8c61\u5b58\u5728\uff0c\u4f18\u5148\u8fd4\u56de\u5b83\u7684 <code>Task<\/code> \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5426\u5219\uff0c\u5f53\u51fa\u73b0\u5f02\u5e38\uff0c\u8fd4\u56de\u7ec8\u6b62\u4e8e\u5f02\u5e38\u7684 <code>STask<\/code> \u3002\uff08\u5e38\u89c1\u4e8e\u7528\u6237\u63d0\u524d\u53d6\u6d88 STask \u7b49\u5f85\u7684\u60c5\u51b5\uff09<\/li>\n\n\n\n<li>\u82e5\u4e0a\u9762\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\uff0c\u8fd4\u56de\u7ed3\u675f\u4e8e\u73b0\u6709\u7ed3\u679c\u7684 <code>STask<\/code> \u3002\uff08\u5e38\u89c1\u4e8e\u4efb\u52a1\u5728\u7b49\u5f85\u524d\u5df2\u7ecf\u5b8c\u6210\u7684\u60c5\u51b5\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>SetException(Excetion)<\/code> \uff1a\u5f53\u4efb\u52a1\u9047\u5230\u5f02\u5e38\uff0c\u8be5\u65b9\u6cd5\u88ab\u72b6\u6001\u673a\u8c03\u7528\u3002<\/li>\n\n\n\n<li><code>SetResult(T)<\/code> \uff1a\u5f53\u4efb\u52a1\u5b8c\u6210\uff0c\u8be5\u65b9\u6cd5\u88ab\u72b6\u6001\u673a\u8c03\u7528\u3002<\/li>\n\n\n\n<li><code>AwaitOnCompleted(ref TAwaiter, ref TStateMachine)<\/code> \uff1a\u5f53\u8be5\u5f02\u6b65\u65b9\u6cd5\uff08\u88ab async \u5173\u952e\u5b57\u6807\u8bb0\uff09\u6267\u884c\u5230 await \u8bed\u53e5\uff0c\u5e76\u4e14 await \u540e\u7684\u5bf9\u8c61\u8fd4\u56de\u7684 awaiter.IsCompleted \u4e3a false\uff08\u8868\u793a\u4efb\u52a1\u672a\u5b8c\u6210\uff0c\u9700\u7b49\u5f85\uff09\uff0c\u5c06\u8c03\u7528\u8be5\u65b9\u6cd5\u3002\u6267\u884c\u5230\u8be5\u65b9\u6cd5\u5c31\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5904\u7406\u5f02\u6b65\u903b\u8f91\uff0c\u56e0\u6b64\u5728\u8fd9\u91cc\u7533\u8bf7 <code>runnerPromise<\/code> \u5bf9\u8c61\uff0c\u5e76\u4e14\u5c06\u56de\u8c03\u65b9\u6cd5\u6ce8\u518c\u7ed9 awaiter\u3002<\/li>\n\n\n\n<li><code>AwaitUnsafeOnCompleted(ref TAwaiter, ref TStateMachine)<\/code> \uff1a\u548c <code>AwaitOnCompleted<\/code> \u7684\u4f5c\u7528\u76f8\u540c\uff0c\u4f46\u6ce8\u518c awaiter \u56de\u8c03\u65f6\u4f7f\u7528\u7684\u662f\u5176 <code>UnsafeOnCompleted(Action)<\/code> \u65b9\u6cd5\u3002\u72b6\u6001\u673a\u4f1a\u6839\u636e awaiter \u5b9e\u73b0\u7684\u63a5\u53e3\u6765\u51b3\u5b9a\u8c03\u7528 <code>AwaitOnCompleted<\/code> \u6216 <code>AwaitUnsafeOnCompleted<\/code> \uff0c\u5177\u4f53\u7684\u9009\u62e9\u7b56\u7565\u6211\u4eec\u5728\u4e0a\u6587 STaskCompletionSourceCore \u5c0f\u8282\u91cc\u6709\u8fc7\u8bf4\u660e\u3002<\/li>\n\n\n\n<li><code>Start(ref TStateMachine)<\/code> \uff1a\u5728 <code>Create()<\/code> \u65b9\u6cd5\u540e\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u542f\u52a8\u72b6\u6001\u673a\uff0c\u542f\u52a8\u540e\u4f1a\u6267\u884c\u5230\uff08\u7b2c\u4e00\u4e2a\uff09await \u8bed\u53e5\u524d\u3002<\/li>\n\n\n\n<li><code>SetStateMachine(IAsyncStateMachine)<\/code> \uff1a <code>builder<\/code> \u88ab\u5b9a\u4e49\u4e3a struct \u7c7b\u578b\u65f6\uff0c\u5728 <code>Create()<\/code> \u65b9\u6cd5\u540e\u88ab\u8c03\u7528\u3002\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4f20\u5165\u4e00\u4e2a\u88c5\u7bb1\u540e\u7684\u72b6\u6001\u673a\u5b9e\u4f8b\uff0c\u76ee\u7684\u662f\u6ee1\u8db3\u7528\u6237\u9700\u8981\u7f13\u5b58\u72b6\u6001\u673a\u5b9e\u4f8b\u7684\u9700\u6c42\uff0c\u4f46\u6211\u4eec\u7528\u4e0d\u4e0a\uff0c\u6240\u4ee5\u8be5\u65b9\u6cd5\u4f53\u7559\u7a7a\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6d4f\u89c8\u8fc7 <code>builder<\/code> \u7684\u6210\u5458\u540e\uff0c\u6709\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u53d1\u73b0\uff0c\u6211\u4eec\u5728\u8bb8\u591a\u5730\u65b9\u90fd\u5224\u65ad\u4e86 <code>runnerPromise<\/code> \u5bf9\u8c61\u662f\u5426\u5b58\u5728\uff08\u662f\u5426\u4e3a\u7a7a\uff09\uff0c\u5374\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48 <code>runnerPromise<\/code> \u5bf9\u8c61\u6709\u4e0d\u5b58\u5728\u7684\u53ef\u80fd\uff0c\u751a\u81f3\u5728\u770b\u8fc7<a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/main\/docs\/features\/task-types.md\">\u5b98\u65b9\u6587\u6863<\/a>\u5bf9 <code>builder<\/code> \u6267\u884c\u903b\u8f91\u7684\u9648\u8ff0\u540e\u66f4\u52a0\u8ff7\u60d1\u2014\u2014&#8221;After <code>Start()<\/code> returns, the <code>async<\/code> method calls <code>builder.Task<\/code> for the task to return from the async method.&#8221;\uff0c\u610f\u601d\u662f\u5728 <code>Start()<\/code> \u65b9\u6cd5\u8fd4\u56de\u540e\uff0c\u72b6\u6001\u673a\u4f1a\u7acb\u5373\u83b7\u53d6 <code>Task<\/code> \u6210\u5458\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u5728\u83b7\u53d6 <code>Task<\/code> \u65f6\uff0c\u600e\u4e48\u786e\u5b9a <code>runnerPromise<\/code> \u662f\u5426\u5b58\u5728\u5462\uff1f<\/p>\n\n\n\n<p>\u5176\u5b9e\u5728 <code>Start()<\/code> \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u8c03\u7528 <code>stateMachine,MoveNext()<\/code> \u65f6\uff0c\u4f1a\u6267\u884c\u5230 await \u8bed\u53e5\uff0c\u5e76\u4e14\u62ff\u5230\u5176\u8fd4\u56de\u7684 awaiter\u3002\u8fd9\u65f6\u6211\u4eec\u4f1a\u5224\u65ad\u8be5 awaiter \u662f\u5426\u5df2\u5b8c\u6210\uff0c\u82e5\u5df2\u5b8c\u6210\uff0c\u5219\u5148\u8c03\u7528 <code>builder.SetResult()<\/code> \u8bbe\u7f6e\u4efb\u52a1\u7ed3\u679c\uff0c\u7136\u540e\u7cfb\u7edf\u624d\u4f1a\u83b7\u53d6 <code>Task<\/code> \uff08\u6b64\u65f6 <code>runnerPromise<\/code> \u4e3a\u7a7a\uff09\uff1b\u82e5\u6ca1\u6709\u5b8c\u6210\uff0c\u5219\u5148\u8c03\u7528 <code>builder.AwaitUnsafeOnCompleted()<\/code> \uff0c\u518d\u83b7\u53d6 <code>Task<\/code> \uff08\u6b64\u65f6 <code>runnerPromise<\/code> \u4e0d\u4e3a\u7a7a\uff09\u3002\u5927\u4f19\u513f\u53ef\u4ee5\u770b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskAsyncMethodBuilder.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"498\" data-original=\"https:\/\/summid.icu\/wp-content\/uploads\/2023\/10\/STaskAsyncMethodBuilder.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1311\"  sizes=\"auto, (max-width: 780px) 100vw, 780px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c0f\u8282<\/h2>\n\n\n\n<p>\u8fd9\u4e00\u7ae0\u6211\u4eec\u6bd4\u8f83\u6df1\u5165\u5730\u8ba8\u8bba\u4e86 STask \u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u867d\u7136\u6587\u7ae0\u5f88\u957f\uff0c\u4f46\u4ecd\u7136\u6ca1\u6709\u529e\u6cd5\u5c06\u91cc\u9762\u7684\u6240\u6709\u7ec6\u8282\u90fd\u8bb2\u89e3\u5230\u3002\u540c\u5b66\u4eec\u5728\u6700\u5f00\u59cb\u9605\u8bfb\u4e0d\u8bba\u662f STask \u8fd8\u662f UniTask \u7684\u6e90\u7801\u65f6\uff0c\u53ef\u4ee5\u5148\u53bb\u611f\u53d7\u5b83\u7684\u5b9e\u73b0\u601d\u60f3\uff0c\u6682\u65f6\u5c06\u4e00\u4e9b\u7ec6\u8282\u653e\u4e00\u8fb9\uff0c\u7b49\u5230\u540e\u9762\u4ea7\u751f\u7591\u95ee\u65f6\uff0c\u518d\u9488\u5bf9\u95ee\u9898\u5177\u4f53\u5206\u6790\u3002\u5bf9\u67d0\u4e9b\u6a21\u5757\u4e0d\u6e05\u695a\u65f6\uff0c\u53ef\u4ee5\u6765\u6587\u7ae0\u5bfb\u627e\u7b54\u6848\uff0c\u83b7\u53d6\u53ef\u4ee5\u7ed9\u4f60\u4e00\u4e9b\u542f\u53d1\u3002\u5f53\u7136\u6587\u7ae0\u4e2d\u82e5\u6709\u9519\u8bef\u6216\u8005\u8868\u8fbe\u4e0d\u660e\u786e\u7684\u5730\u65b9\uff0c\u8bf7\u52a1\u5fc5\u6307\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7684 STask \u53ea\u5b9e\u73b0\u4e86\u57fa\u672c\u7684\u201c\u5f02\u6b65\u65b9\u6cd5\u201d\u903b\u8f91\uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u7684\u7406\u7531\uff0c\u4e5f\u6682\u65f6\u53ea\u6709\u6bd4\u4f20\u7edf Task \u5f02\u6b65\u65b9\u6cd5\u6027\u80fd\u66f4\u597d\u8fd9\u4e00\u4e2a\u7406\u7531\u2014\u2014\u8fd9\u8fdc\u8fdc\u662f\u4e0d\u591f\u7684\u3002\u56e0\u6b64\u5728\u4e0b\u4e00\u7ae0\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u6269\u5c55 STask\uff0c\u8ba9\u6211\u4eec\u8fed\u4ee3\u4f20\u7edf\u6253\u6cd5\uff0c\u4f18\u5316\u5de5\u5177\u611f\u77e5\u5ea6\uff0c\u8d4b\u80fd\u4e1a\u52a1\u5f00\u53d1\u6d41\u7a0b\uff01~o( =\u2229\u03c9\u2229= )<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u89c8 \u6b63\u5f0f\u7f16\u5199\u4ee3\u7801\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5f02\u6b65\u6846\u67b6\u6709\u4e00\u4e2a\u5927\u4f53\u7684\u8ba4\u8bc6\uff0c\u56e0\u6b64\u6211\u7b80\u5355\u753b\u4e86\u4e00\u5e45\u7c7b\u56fe\uff0c\u5e2e\u52a9\u5927\u5bb6\u7559\u4e2a\u5370\u8c61\uff1a \u9664\u4e86\u63a5\u53e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[23,20],"class_list":["post-1320","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c","tag-unity"],"_links":{"self":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/comments?post=1320"}],"version-history":[{"count":3,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1320\/revisions"}],"predecessor-version":[{"id":1323,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/1320\/revisions\/1323"}],"wp:attachment":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/media?parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/categories?post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/tags?post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}