{"id":867,"date":"2021-09-14T11:10:51","date_gmt":"2021-09-14T03:10:51","guid":{"rendered":"http:\/\/www.wayln.com\/?p=867"},"modified":"2021-09-14T11:10:51","modified_gmt":"2021-09-14T03:10:51","slug":"system-reactive%e5%9f%ba%e7%a1%80-%e5%90%91%e4%b8%8a%e4%b8%8b%e6%96%87%e5%8f%91%e9%80%81%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=867","title":{"rendered":"System.Reactive\u57fa\u7840\u2014\u2014\u5411\u4e0a\u4e0b\u6587\u53d1\u9001\u901a\u77e5"},"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>System.Reactive\u6700\u5927\u9650\u5ea6\u5730\u5b9e\u73b0\u4e86\u65e0\u5dee\u522b\u5bf9\u5f85\u4e0d\u540c\u7684\u7ebf\u7a0b\u3002\u65e0\u8bba\u5f53\u524d\u662f\u4ec0\u4e48\u7ebf\u7a0b\uff0c\u5b83\u90fd\u4f1a\u5728\u5176\u4e2d\u53d1\u9001\u901a\u77e5\u3002\u6bd4\u5982OnNext.\u6bcf\u4e2aOnNext\u901a\u77e5\u90fd\u662f\u6309\u987a\u5e8f\u51fa\u73b0\u7684\uff0c\u4f46\u672a\u5fc5\u603b\u5728\u76f8\u540c\u7684\u7ebf\u7a0b\u4e2d<br \/>\n\u5047\u8bbe\u8981\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u4e0a\u9650\u4e3a\u4e2d\u53d1\u9001\u901a\u77e5\uff0c\u6bd4\u5982UI\u5143\u7d20\u53ea\u5e94\u5728\u5305\u542b\u4ed6\u4eec\u7684UI\u7ebf\u7a0b\u4e2d\u64cd\u4f5c\uff0c\u5982\u679c\u6709\u901a\u77e5\u4ece\u7ebf\u7a0b\u6c60\u4f20\u6765\uff0c\u800c\u4f60\u6b63\u5728\u66f4\u65b0\u54cd\u5e94\u74f7\u901a\u77e5\u7684UI\uff0c\u5219\u9700\u8981\u79fb\u6b65\u81f3UI\u7ebf\u7a0b<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>System.Reactiv\u63d0\u4f9b\u7684ObServeOn\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u628a\u901a\u77e5\u8f6c\u79fb\u5230\u5176\u4ed6\u8c03\u5ea6\u5668\u4e2d\u3002<br \/>\n\u89c2\u5bdf\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5176\u4e2d\u7684Interval\u8fd0\u7b97\u7b26\u6bcf\u8fc7\u4e00\u79d2\u53d1\u9001\u4e00\u6b21OnNext\u901a\u77e5\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">private void Button_Click\uff08object sender,RoutedEventArgs e)\n{\n     Trace.WriteLine(<span class=\"katex math inline\">\"UI thread is {Environment.CurrentManagedThreadId}\");\n            Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(\n                x =&gt; Trace.WriteLine(<\/span>\"Interval{x} on thread {Environment.CurrentManagedThreadId}\"));\n}\n<\/code><\/pre>\n<p>\u4ee5\u6211\u7684\u8ba1\u7b97\u673a\u4e3a\u4f8b\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">Interval0 on thread 5\nInterval1 on thread 5\nInterval2 on thread 4\nInterval3 on thread 5\nInterval4 on thread 4\nInterval5 on thread 4\nInterval6 on thread 4\nInterval7 on thread 4\nInterval8 on thread 4\nInterval9 on thread 4\n\n<\/code><\/pre>\n<p>\u56e0\u4e3aInterval\u57fa\u4e8e\u8ba1\u65f6\u5668\uff08\u6ca1\u6709\u7279\u5b9a\u7684\u7ebf\u7a0b\uff09\uff0c\u6240\u4ee5\u901a\u77e5\u6765\u81ea\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\uff0c\u800c\u975eUI\u7ebf\u7a0b\uff0c\u5982\u679c\u9700\u8981\u66f4\u65b0UI\u5143\u7d20\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7ObserveOn\u4f20\u9012\u8fd9\u4e9b\u901a\u77e5\uff0c\u5e76\u4f20\u9012\u4e00\u4e2a\u8868\u793aUI\u7ebf\u7a0b\u7684\u540c\u6b65\u4e0a\u4e0b\u6587\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\"> private void button4_Click(object sender, EventArgs e)\n        {\n            SynchronizationContext uiContext = SynchronizationContext.Current;\n            Trace.WriteLine(<span class=\"katex math inline\">\"UI thread is {Environment.CurrentManagedThreadId}\");\n            Observable.Interval(TimeSpan.FromSeconds(1)).ObserveOn(uiContext).Subscribe(\n                x =&gt; Trace.WriteLine(<\/span>\"Interval {x} on thread {Environment.CurrentManagedThreadId}\"));\n\n<\/code><\/pre>\n<p>ObserveOn\u7684\u53e6\u4e00\u4e2a\u5e38\u89c1\u7528\u6cd5\u662f\u5728\u5fc5\u8981\u65f6\u79bb\u5f00UI\u7ebf\u7a0b\u3002\u5047\u8bbe\u5728\u67d0\u79cd\u60c5\u51b5\u4e0b\uff0c\u6bcf\u5f53\u79fb\u52a8\u9f20\u6807\u65f6\uff0c\u4fbf\u9700\u8981\u8fdb\u884cCPU\u5bc6\u96c6\u578b\u8ba1\u7b97\u3002\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u9f20\u6807\u79fb\u52a8\u90fd\u6765\u81eaUI\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7ObserveOn\u628a\u8fd9\u4e9b\u901a\u77e5\u8f6c\u79fb\u5230\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\uff0c\u7136\u540e\u8fdb\u884c\u8ba1\u7b97\uff0c\u6700\u540e\u628a\u7ed3\u679c\u901a\u8fc7\u901a\u77e5\u6298\u8fd4\u5230UI\u7ebf\u7a0b\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">            SynchronizationContext uiContext = SynchronizationContext.Current;\n            Trace.WriteLine(<span class=\"katex math inline\">\"UI thread is {Environment.CurrentManagedThreadId}\");\n            Observable.FromEventPattern&lt;MouseEventHandler, MouseEventArgs&gt;(\n                handler =&gt; (s, a) =&gt; handler(s, a),\n                handler =&gt; MouseMove += handler,\n                handler =&gt; MouseMove -= handler\n                ).Select(evt =&gt; evt.EventArgs.GetPosition(this)).ObserveOn(Scheduler.Default).Select(position =&gt;\n                  {\n                      Thread.Sleep(100);\n                      var result = position.X + position.Y;\n                      var thread = Environment.CurrentManagedThreadId;\n                      Trace.WriteLine(<\/span>\"Calculated result {result} on thread {thread}\");\n                      return result;\n                  }).ObserverOn(uiContext).Subscribe(x =&gt; Trace.WriteLine($\"Result {x} on thread {Environment.CurrentManagedThreadId}\"));\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>ObserveOn\u4f1a\u628a\u901a\u77e5\u79fb\u81f3System.Reactive\u8c03\u5ea6\u5668\uff0c\u672c\u8282\u6240\u8ff0\u7684\u53ea\u662f\u9ed8\u8ba4\u7684\u7ebf\u7a0b\u6c60\u8c03\u5ea6\u5668\u548c\u521b\u5efaUI\u8c03\u5ea6\u5668\u7684\u4e00\u79cd\u65b9\u5f0f\u800c\u5df2\u3002ObserveOn\u8fd0\u7b97\u7b26\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u8fdb\u51fa\u4e8eUI\u7ebf\u7a0b\uff0c\u4f46\u7535\u9540\u6f06\u5728\u5176\u4ed6\u573a\u666f\u4e2d\u4e5f\u5f88\u6709\u7528\u3002<br \/>\nObserveOn\u63a7\u5236\u53ef\u89c2\u5bdf\u901a\u77e5\u7684\u4e0a\u4e0b\u6587\uff0c\u8bf7\u52ff\u5c06\u5b83\u548cSubscribeOn\u6df7\u6dc6\uff0c\u540e\u8005\u63a7\u5236\u7684\u662f\u8d1f\u8d23\u6dfb\u52a0\u548c\u79fb\u9664\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7684\u4ee3\u7801\u6240\u5728\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 System.Reactive\u6700\u5927\u9650\u5ea6\u5730\u5b9e\u73b0\u4e86\u65e0\u5dee\u522b\u5bf9\u5f85\u4e0d [&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-867","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\/867","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=867"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/867\/revisions"}],"predecessor-version":[{"id":868,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/867\/revisions\/868"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}