{"id":4156,"date":"2021-01-09T19:44:26","date_gmt":"2021-01-09T10:44:26","guid":{"rendered":"https:\/\/okamurax.com\/?p=4156"},"modified":"2021-01-09T19:44:26","modified_gmt":"2021-01-09T10:44:26","slug":"c-task-cancellationtoken%e3%81%a7%e3%82%ad%e3%83%a3%e3%83%b3%e3%82%bb%e3%83%ab","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=4156","title":{"rendered":"C# Task\/CancellationToken\u3067\u30ad\u30e3\u30f3\u30bb\u30eb"},"content":{"rendered":"<pre class=\"lang:c# decode:true \">using System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n\r\nnamespace WindowsFormsApp1\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            var f = new TestForm();\r\n            f.Show();\r\n\r\n            \/\/ Form1\u304b\u3089TestForm\u3092\u8d77\u52d5\u30575\u79d2\u5f8c\u306bTestForm\u306eText\u3092\u8a2d\u5b9a\u3059\u308b\u3002\r\n            \/\/ \u5f53\u7136\u3001Text\u8a2d\u5b9a\u524d\u306bTestForm\u3092\u9589\u3058\u308c\u3070\u3001Invoke\u3067\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u3002\r\n            \/\/ CancellationToken\u3067\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u3002\r\n        }\r\n    }\r\n\r\n    class TestForm :Form\r\n    {\r\n        public TestForm()\r\n        {\r\n            var source = new System.Threading.CancellationTokenSource();\r\n            var token = source.Token;\r\n\r\n            this.FormClosing += (s, e) =&gt; {\r\n                source.Cancel();\r\n                source.Dispose();\r\n            };\r\n\r\n            Task.Run(() =&gt;\r\n            {\r\n                System.Threading.Thread.Sleep(5000);\r\n                if(!token.IsCancellationRequested) this.Invoke((MethodInvoker)(() =&gt; { this.Text = \"Hello\"; }));\r\n\r\n            }, token);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Fo &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=4156\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Task\/CancellationToken\u3067\u30ad\u30e3\u30f3\u30bb\u30eb&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-4156","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4156"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4156\/revisions"}],"predecessor-version":[{"id":4157,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4156\/revisions\/4157"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}