{"id":869,"date":"2021-09-14T11:50:14","date_gmt":"2021-09-14T03:50:14","guid":{"rendered":"http:\/\/www.wayln.com\/?p=869"},"modified":"2021-09-14T11:50:14","modified_gmt":"2021-09-14T03:50:14","slug":"system-reactive%e5%9f%ba%e7%a1%80-%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%92%8c%e7%bc%93%e5%86%b2%e6%9d%a5%e5%88%86%e7%bb%84%e4%ba%8b%e4%bb%b6%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=869","title":{"rendered":"System.Reactive\u57fa\u7840\u2014\u2014\u4f7f\u7528\u7a97\u53e3\u548c\u7f13\u51b2\u6765\u5206\u7ec4\u4e8b\u4ef6\u6570\u636e"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u95ee\u9898<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u89e3\u51b3\u65b9\u6848<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_1\">3<\/span> \u8ba8\u8bba<\/a><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u95ee\u9898<\/span><\/h1>\n<p>\u4e00\u79cd\u60c5\u51b5\u662f\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8b\u4ef6\u5e8f\u5217\uff0c\u9700\u8981\u5728\u6536\u5230\u4e8b\u4ef6\u65f6\u5c06\u4ed6\u4eec\u5206\u7ec4\uff0c\u6bd4\u5982\u9700\u8981\u54cd\u5e94\u6210\u5bf9\u7684\u8f93\u5165\u3002\u53e6\u4e00\u79cd\u60c5\u51b5\u662f\uff0c\u5047\u8bbe\u9700\u8981\u5728\u4e24\u79d2\u7684\u7a97\u53e3\u5185\u54cd\u5e94\u6240\u6709\u7684\u8f93\u5165\u3002<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>System.Reactive\u6709\u4e24\u4e2a\u80fd\u591f\u5bf9\u51fa\u5165\u7684\u5e8f\u5217\u8fdb\u884c\u5206\u7ec4\u7684\u8fd0\u7b97\u7b26\uff0c\u5206\u522b\u662fBuffer(\u7f13\u51b2)\u548cWindow\uff08\u7a97\u53e3\uff09\u3002Buffer\u4f1a\u4fdd\u7559\u4f20\u5165\u7684\u4e8b\u4ef6\uff0c\u76f4\u81f3\u8be5\u7ec4\u5b8c\u6210\uff0c\u6b64\u65f6\u4ed6\u4f1a\u628a\u5168\u90e8\u4e8b\u4ef6\u4f5c\u4e3a\u4e00\u4e2a\u4e8b\u4ef6\u96c6\u5408\u8f6c\u53d1\u3002Window\u4f1a\u6309\u903b\u8f91\u5bf9\u8f93\u5165\u4e8b\u4ef6\u5206\u7ec4\uff0c\u4f46\u4f1a\u5728\u65f6\u95f4\u5230\u6765\u65f6\u5c31\u628a\u4ed6\u4eec\u4f20\u9012\u51fa\u53bb\u3002Buffer\u7684\u8fd4\u56de\u7c7b\u578b\u662fIObservable&lt;IList&lt;T&#62;>\uff08\u96c6\u5408\u7684\u4e8b\u4ef6\u6d41\uff09\uff0cWindow\u7684\u8fd4\u56de\u7c7b\u578b\u662fIObservable&lt;IObservable<T\/>>(\u4e8b\u4ef6\u6d41\u7684\u4e8b\u4ef6\u6d41)\u3002<br \/>\n\u4e0b\u9762\u7684\u793a\u4f8b\u4f7f\u7528Interval\u8fd0\u7b97\u7b26\uff0c\uff0c\u6bcf\u79d2\u521b\u5efa\u4e00\u6b21OnNext\u901a\u77e5\uff0c\u6bcf\u6b21\u7f13\u51b2\u4e24\u4e2a\u901a\u77e5:<\/p>\n<p>&#8220;`CSharp Observable.Interval(TimeSpan.FromSeconds(1)).Buffer(2).Subscribe(<br \/>\n                x =&gt; Trace.WriteLine($&quot;{DateTime.Now.Second}: Got {x[0]} and {x[1]}&quot;));<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;line-numbers&quot;&gt;\u4ee5\u6211\u7684\u7535\u8111\u4e3a\u4f8b\uff0c\u4ee3\u7801\u6bcf\u4e24\u79d2\u751f\u6210\u4e00\u5bf9\u8f93\u51fa\uff1a<br \/>\n&#8220;`CSharp<br \/>\n20: Got 0 and 1<br \/>\n22: Got 2 and 3<br \/>\n24: Got 4 and 5<br \/>\n26: Got 6 and 7<br \/>\n28: Got 8 and 9<br \/>\n30: Got 10 and 11<br \/>\n32: Got 12 and 13<br \/>\n<\/code><\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u4f3c\u7684\u793a\u4f8b\u4f7f\u7528Window\u6765\u521b\u5efa\u4eae\u4eae\u4e00\u5bf9\u7684\u4e8b\u4ef6\u5206\u7ec4\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">            Observable.Interval(TimeSpan.FromSeconds(1)).Window(2).Subscribe(\n                group =&gt;\n                {\n                    Trace.WriteLine(<span class=\"katex math inline\">\"{DateTime.Now.Second}: Starting new group\");\n                    group.Subscribe(x=&gt;Trace.WriteLine(<\/span>\"{DateTime.Now.Second}; Saw{x}\"),\n                        ()=&gt;Trace.WriteLine($\"{DateTime.Now.Second}; Ending group\"));\n                });\n<\/code><\/pre>\n<p>\u4ee5\u6211\u7684\u8ba1\u7b97\u673a\u4e3a\u4f8b\uff0c\u8fd9\u4e2aWindow\u793a\u4f8b\u751f\u6210\u4e86\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">54: Starting new group\n55; Saw0\n56; Saw1\n56; Ending group\n56: Starting new group\n57; Saw2\n58; Saw3\n58; Ending group\n58: Starting new group\n59; Saw4\n0; Saw5\n0; Ending group\n0: Starting new group\n1; Saw6\n2; Saw7\n2; Ending group\n2: Starting new group\n3; Saw8\n4; Saw9\n<\/code><\/pre>\n<p>Buffer\u7b49\u5f85\u5176\u5206\u7ec4\u4e2d\u7684\u6240\u6709\u4e8b\u4ef6\uff0c\u7136\u540e\u53d1\u5e03\u5355\u4e2a\u96c6\u5408\uff0cwindows\u4e8b\u4ef6\u5206\u7ec4\u65b9\u5f0f\u4e0e\u4e4b\u76f8\u540c\uff0c\u4f46\u4f1a\u5728\u4e16\u754c\u4f20\u5165\u65f6\u4fbf\u53d1\u5e03\u4ed6\u4eec\uff0c\u53e6\u5916\uff0cWindow\u4f1a\u7acb\u5373\u53d1\u5e03\u53ef\u89c2\u5bdf\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u4f1a\u4e3a\u8be5\u7a97\u53e3\u53d1\u5e03\u4e8b\u4ef6\u3002<br \/>\nBuffer\u548cWindow\u5747\u53ef\u7528\u4e8e\u65f6\u95f4\u8de8\u5ea6\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">        private void button7_Click(object sender, EventArgs e)\n        {\n            Observable.FromEventPattern&lt;MouseEventHandler, MouseEventArgs&gt;(\n\n                handler =&gt; (s, a) =&gt; handler(s, a),\n\n                handler =&gt; MouseMove += handler,\n                handler =&gt; MouseMove -= handler).Buffer(TimeSpan.FromSeconds(1)).Subscribe(\n                x =&gt; Trace.WriteLine($\"{DateTime.Now.Second}:Saw {x.Count} items.\"));\n        }\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5229\u7528Buffer\u548cWindow\u8fd9\u6837\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u8f93\u5165\u5851\u9020\u6210\u671f\u671b\u770b\u5230\u7684\u6837\u5b50\u3002\u53e6\u4e00\u4e2a\u9002\u7528\u7684\u6280\u5de7\u662f\u8282\u6d41<br \/>\nBuffer\u548cWindow\u90fd\u6709\u5176\u4ed6\u7684\u91cd\u8f7d\uff0c\u53ef\u4ee5\u5728\u66f4\u9ad8\u573a\u666f\u4e2d\u4f7f\u7528\u3002\u5e26\u6709Skip\u53c2\u6570\u548ctimeShit\u53c2\u6570\u7684\u91cd\u8f7d\u65e2\u53ef\u4ee5\u521b\u5efa\u4e0e\u5176\u4ed6\u5206\u7ec4\u91cd\u53e0\u7684\u5206\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u5728\u7ec4\u4e0e\u7ec4\u4e4b\u95f4\u8df3\u8fc7\u5143\u7d20\u3002\u8fd8\u6709\u4e00\u4e9b\u91cd\u8f7d\u63a5\u53d7\u59d4\u6258\uff0c\u53ef\u7528\u4e8e\u52a8\u6001\u5b9a\u4e49\u5206\u7ec4\u7684\u8fb9\u754c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u4e00\u79cd\u60c5\u51b5\u662f\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8b\u4ef6\u5e8f\u5217\uff0c\u9700\u8981\u5728\u6536\u5230\u4e8b\u4ef6\u65f6\u5c06\u4ed6\u4eec\u5206\u7ec4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,41,2],"tags":[],"class_list":["post-869","post","type-post","status-publish","format-standard","hentry","category-c","category-41","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=869"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions\/870"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}