{ "sheetModels": [ { "title": "Sheet1", "rootTopicKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "editorRootTopicKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "focusKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "topics": [ { "key": "7c87ba44-1ebc-4584-b2c7-017c5023646a", "parentKey": "ec5d60a5-b6aa-435b-84fe-954c765fdd03", "subKeys": [ "553ce375-6c24-4445-9860-b08d2e447bb4" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "描述一个任务" } ] }, { "key": "aa6d6e77-689a-477a-9f2a-a62ac019bdac", "parentKey": "6baa9033-8a72-4ac5-a135-c24115383114", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "Main函数可以被标记为async,但同时返回值不能是void" } ] }, { "key": "1ed55f14-5c50-4e2d-bc6f-b1eeff18834b", "parentKey": "84b4b363-729e-4f04-97cd-bb8054699cbe", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "Task可以等待" } ] }, { "key": "de3778bc-e7d8-4729-9127-c6047900fc53", "parentKey": "c7b99a7f-8d73-464e-9554-56fda8b5f4b0", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "想用同步的方式写出异步的代码" } ] }, { "key": "6baa9033-8a72-4ac5-a135-c24115383114", "parentKey": "39092d0e-0899-44d6-8f10-af59da71c2bf", "subKeys": [ "aa6d6e77-689a-477a-9f2a-a62ac019bdac" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "这个关键字用于标记函数为异步操作" } ] }, { "key": "8af5835d-753d-45dd-b6a7-0ac8ebbacfe9", "parentKey": "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "subKeys": [ "33f69f47-8b09-49c2-9543-492625778cb8", "50dfabc8-c44d-4645-9b78-efee70e98cac", "81929177-4603-4595-89b0-fdd624cd188d" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "[疑问]在async函数内部,推荐使用await,为什么" } ] }, { "key": "0c7b341d-24f2-4f45-9919-4e22a4451b65", "parentKey": "b87d175e-fef8-4770-949b-7568079230b6", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "调用者不会等待其执行完成,就像在CUDA中的GPU函数" } ] }, { "key": "7a0dbea5-ccb0-4c99-8451-e045e3fdd586", "parentKey": "713dacf8-ed11-46ca-aeb0-8db84fdbeb8c", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "在调用处编译器也不会提示要等待其完成(因为不可等待)" } ] }, { "key": "b89ef0fe-6cbd-4184-84d0-59f33c470c01", "parentKey": "ec5d60a5-b6aa-435b-84fe-954c765fdd03", "subKeys": [ "58513c8f-62b6-4a87-b791-10a9fb49044c", "563f3fd8-f92f-497f-ae45-985b10ae9664" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "创建方式" } ] }, { "key": "50dfabc8-c44d-4645-9b78-efee70e98cac", "parentKey": "8af5835d-753d-45dd-b6a7-0ac8ebbacfe9", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "将函数主体移动到await Task.Run()中可以去掉警告" } ] }, { "key": "8df3a262-623a-47df-ad1f-66fafb8f06c5", "parentKey": "6574a5f8-4888-439b-8bc2-666a723884da", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "返回值类型应该是:Task<int>" } ] }, { "key": "39092d0e-0899-44d6-8f10-af59da71c2bf", "parentKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "subKeys": [ "6baa9033-8a72-4ac5-a135-c24115383114", "b87d175e-fef8-4770-949b-7568079230b6" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "async" } ] }, { "key": "ad8a604a-21b4-4dba-8736-dbafa117d58f", "parentKey": "1b1c1ded-05b5-408a-8397-0f7838c9abd0", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "在调用处,编译器会提示你应该等待返回的Task完成" } ] }, { "key": "1b1c1ded-05b5-408a-8397-0f7838c9abd0", "parentKey": "5eefb1a7-5115-4653-b9d2-b8fdd4a9108f", "subKeys": [ "f4b978fd-ccfe-4283-89ce-94a5f0c6419e", "ad8a604a-21b4-4dba-8736-dbafa117d58f" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "可以是不带模板类型的Task,这样这个函数就是可等待的" } ] }, { "key": "713dacf8-ed11-46ca-aeb0-8db84fdbeb8c", "parentKey": "5eefb1a7-5115-4653-b9d2-b8fdd4a9108f", "subKeys": [ "7a0dbea5-ccb0-4c99-8451-e045e3fdd586", "c9f39cc6-42a1-481a-ae6d-306c3b4a93e9" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "返回值类型可以直接是void,这样这个函数开始执行之后,就相当是完全“断了联系”" } ] }, { "key": "c9f39cc6-42a1-481a-ae6d-306c3b4a93e9", "parentKey": "713dacf8-ed11-46ca-aeb0-8db84fdbeb8c", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "应该是不太常用的方式,因为异步操作的结束时机未知" } ] }, { "key": "27006c25-1325-4a33-8c9c-f0ad81738fa2", "parentKey": "6574a5f8-4888-439b-8bc2-666a723884da", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "比如:函数的计算结果是int,要将其返回" } ] }, { "key": "06449778-a6fb-403d-b7e2-a4b68b52d4d1", "parentKey": "5f73e68b-f117-426d-bf1f-378e4647afc1", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "在非async的函数内部,不能使用await,但是仍然可以使用Task.Wait()函数同步等待其执行结束" } ] }, { "key": "833afe3a-7645-4c90-812b-4b3c56fa4353", "parentKey": "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "在一个async函数内部,可以使用多个await" } ] }, { "key": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "parentKey": null, "subKeys": [ "b8bdbcf1-69c0-46c0-9894-b63a24a6532a", "39092d0e-0899-44d6-8f10-af59da71c2bf", "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "ec5d60a5-b6aa-435b-84fe-954c765fdd03" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "C#异步(async)" } ] }, { "key": "563f3fd8-f92f-497f-ae45-985b10ae9664", "parentKey": "b89ef0fe-6cbd-4184-84d0-59f33c470c01", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "异步函数(async)可以返回Task" } ] }, { "key": "ad31400c-5173-47a3-be0e-2d9de1291208", "parentKey": "5f73e68b-f117-426d-bf1f-378e4647afc1", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "是不是这个限制没有什么意义?" } ] }, { "key": "b8bdbcf1-69c0-46c0-9894-b63a24a6532a", "parentKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "subKeys": [ "c7b99a7f-8d73-464e-9554-56fda8b5f4b0" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "概述" } ] }, { "key": "75c1dddd-6b7a-426f-8095-398bbf42972a", "parentKey": "ec5d60a5-b6aa-435b-84fe-954c765fdd03", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "Task是基于线程池的" } ] }, { "key": "f4b978fd-ccfe-4283-89ce-94a5f0c6419e", "parentKey": "1b1c1ded-05b5-408a-8397-0f7838c9abd0", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "即使其返回值类型是Task,也不需要显式的return语句" } ] }, { "key": "b87d175e-fef8-4770-949b-7568079230b6", "parentKey": "39092d0e-0899-44d6-8f10-af59da71c2bf", "subKeys": [ "0c7b341d-24f2-4f45-9919-4e22a4451b65", "5eefb1a7-5115-4653-b9d2-b8fdd4a9108f", "6574a5f8-4888-439b-8bc2-666a723884da", "84b4b363-729e-4f04-97cd-bb8054699cbe" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "异步函数" } ] }, { "key": "6574a5f8-4888-439b-8bc2-666a723884da", "parentKey": "b87d175e-fef8-4770-949b-7568079230b6", "subKeys": [ "27006c25-1325-4a33-8c9c-f0ad81738fa2", "8df3a262-623a-47df-ad1f-66fafb8f06c5", "80967d11-c6bb-44f2-be53-c48e0d687b40", "cc48fd0f-84a5-433a-bab1-8e2547aae06e" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "有返回值的话,必须以Task<T>的形式返回" } ] }, { "key": "84b4b363-729e-4f04-97cd-bb8054699cbe", "parentKey": "b87d175e-fef8-4770-949b-7568079230b6", "subKeys": [ "13804f37-bdc8-484e-a8b0-061696a0476f", "1ed55f14-5c50-4e2d-bc6f-b1eeff18834b", "7f268bf6-fbb4-437f-9ef7-b44f615d9ba4" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "返回值总结" } ] }, { "key": "33f69f47-8b09-49c2-9543-492625778cb8", "parentKey": "8af5835d-753d-45dd-b6a7-0ac8ebbacfe9", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "如果我这个函数本身就在进行很复杂的运算,并不是等待其他操作完成呢?感觉这样没有必要一定使用await" } ] }, { "key": "5f73e68b-f117-426d-bf1f-378e4647afc1", "parentKey": "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "subKeys": [ "06449778-a6fb-403d-b7e2-a4b68b52d4d1", "ad31400c-5173-47a3-be0e-2d9de1291208" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "[疑问]await只能用于async函数内部,考虑一下为什么这么设计" } ] }, { "key": "13804f37-bdc8-484e-a8b0-061696a0476f", "parentKey": "84b4b363-729e-4f04-97cd-bb8054699cbe", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "void不可等待" } ] }, { "key": "cc48fd0f-84a5-433a-bab1-8e2547aae06e", "parentKey": "6574a5f8-4888-439b-8bc2-666a723884da", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "函数调用者接收到的返回值是Task<int>" } ] }, { "key": "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "parentKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "subKeys": [ "9f169741-d236-416e-858c-9ddfce668d71", "833afe3a-7645-4c90-812b-4b3c56fa4353", "5f73e68b-f117-426d-bf1f-378e4647afc1", "8af5835d-753d-45dd-b6a7-0ac8ebbacfe9" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "await" } ] }, { "key": "80967d11-c6bb-44f2-be53-c48e0d687b40", "parentKey": "6574a5f8-4888-439b-8bc2-666a723884da", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "返回语句应该是:return some_int;" } ] }, { "key": "9f169741-d236-416e-858c-9ddfce668d71", "parentKey": "2f9e65d4-3152-4662-abcf-bca0fb9b69b0", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "等待一个Task (awaitable)完成" } ] }, { "key": "ec5d60a5-b6aa-435b-84fe-954c765fdd03", "parentKey": "ac3d1bfa-dfdd-4128-b729-b50ac80082c7", "subKeys": [ "7c87ba44-1ebc-4584-b2c7-017c5023646a", "b89ef0fe-6cbd-4184-84d0-59f33c470c01", "75c1dddd-6b7a-426f-8095-398bbf42972a" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "Task" } ] }, { "key": "c7b99a7f-8d73-464e-9554-56fda8b5f4b0", "parentKey": "b8bdbcf1-69c0-46c0-9894-b63a24a6532a", "subKeys": [ "de3778bc-e7d8-4729-9127-c6047900fc53" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "设计目的" } ] }, { "key": "58513c8f-62b6-4a87-b791-10a9fb49044c", "parentKey": "b89ef0fe-6cbd-4184-84d0-59f33c470c01", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "可以通过Task.Run()手动创建任务" } ] }, { "key": "81929177-4603-4595-89b0-fdd624cd188d", "parentKey": "8af5835d-753d-45dd-b6a7-0ac8ebbacfe9", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "[存疑]在async函数内部,如果没有await就是普通的同步方法" } ] }, { "key": "7f268bf6-fbb4-437f-9ef7-b44f615d9ba4", "parentKey": "84b4b363-729e-4f04-97cd-bb8054699cbe", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "Task<T>可以等待并且有结果" } ] }, { "key": "5eefb1a7-5115-4653-b9d2-b8fdd4a9108f", "parentKey": "b87d175e-fef8-4770-949b-7568079230b6", "subKeys": [ "713dacf8-ed11-46ca-aeb0-8db84fdbeb8c", "1b1c1ded-05b5-408a-8397-0f7838c9abd0" ], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "可以没有返回值" } ] }, { "key": "553ce375-6c24-4445-9860-b08d2e447bb4", "parentKey": "7c87ba44-1ebc-4584-b2c7-017c5023646a", "subKeys": [], "collapse": false, "style": null, "blocks": [ { "type": "CONTENT", "data": "基础操作包括:WhenAll / WhenAny / Wait / Start" } ] } ], "config": { "viewMode": "MindMap", "readOnly": false, "allowUndo": true, "layoutDir": 0, "theme": { "name": "defaultTheme", "randomColor": false, "background": "#DDDDDD", "highlightColor": "#C31004", "marginH": 50, "marginV": 5, "contentStyle": { "lineHeight": "1" }, "linkStyle": { "lineRadius": 5, "lineWidth": "2px", "lineColor": "#595959", "lineType": "curve" }, "rootTopic": { "contentStyle": { "background": "#C31004", "color": "#fff", "fontSize": "34px", "borderRadius": "5px", "padding": "16px 18px 16px 18px" }, "subLinkStyle": { "lineType": "curve" } }, "primaryTopic": { "contentStyle": { "background": "#333", "borderRadius": "5px", "color": "#fff", "fontSize": "14px", "padding": "6px 10px 5px 10px" }, "subLinkStyle": { "hasUnderline": true, "lineType": "round" } }, "normalTopic": { "contentStyle": { "borderRadius": "5px", "background": "#fff0", "color": "#383833", "fontSize": "13px", "padding": "1px" }, "subLinkStyle": { "hasUnderline": true, "lineType": "round" } } } } } ], "extData": {}, "currentSheetIndex": 0, "formatVersion": "0.1" }