{"id":280959,"date":"2016-11-15T06:20:04","date_gmt":"2016-11-15T03:20:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=280959"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=280959","title":{"rendered":"\u041a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043d\u0430 \u0442\u0430\u0439\u043c\u0435\u0440\u0430\u0445"},"content":{"rendered":"<p>\u0412 <a href=\"https:\/\/habrahabr.ru\/post\/313824\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043f\u0440\u0438\u0432\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u0439\u0441\u044f \u043f\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044e \u043e\u0442 <b>DispatcherTimer<\/b>. \u041c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u0438\u0434\u0435\u044e \u0434\u0430\u043b\u044c\u0448\u0435. \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c\u0438 \u0442\u0430\u0439\u043c\u0435\u0440\u0430\u043c\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0441 \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e. \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439, <br \/>  \u0442\u0430\u043a \u0438 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430, \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u043c\u0435\u0436\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<p>  \u041a\u0430\u043a \u0438 <b>async\/await<\/b>, \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0441 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435\u043c, \u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u0448\u0438\u0440\u043e\u043a\u0443\u044e \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e- \u0438 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430, \u043d\u043e \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0434\u043e\u043b\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u0445\u043e\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434:  <\/p>\n<pre><code>  public struct ProgressInfo {     public string Message;     public int Current, Total;   }   async Task Foo(IProgress&lt;ProgressInfo&gt; progress, CancellationToken ct) <\/code><\/pre>\n<p>  \u041c\u044b \u0436\u0435 \u0434\u043e\u0432\u0435\u0440\u0438\u043c \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c\u0443 \u043a\u043e\u0434\u0443, \u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c, \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438.  <\/p>\n<pre><code>  public static IEnumerable&lt;object&gt; Process&lt;T&gt;(this ICollection&lt;T&gt; collection, Action&lt;T&gt; action) {     ProgressInfo info = new ProgressInfo() { Total = collection.Count };     foreach (var item in collection) {       action(item);       info.Current++;       info.Message = string.Format(&quot;Processed {0:d} from {1:d}&quot;, info.Current, info.Total);       yield return info;     }     yield break;   } <\/code><\/pre>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u044b\u0437\u043e\u0432 \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u0437 \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430.   <\/p>\n<pre><code>  IEnumerable&lt;object&gt; Foo() { yield break; }   IEnumerable&lt;object&gt; Bar() { yield break; }   IEnumerable&lt;object&gt; Baz() {     foreach (var a in Foo()) yield return a;     foreach (var b in Bar()) yield return b;     yield break;   } <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u043c \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0431\u0443\u0434\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440:  <\/p>\n<pre><code>  IEnumerable&lt;object&gt; Baz() {     yield return Foo();     yield return Bar();     yield break;   } <\/code><\/pre>\n<p>  \u0410 \u043e \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0438 \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434:  <\/p>\n<pre><code>  public static IEnumerable&lt;object&gt; Flat(this IEnumerable&lt;object&gt; cot) {     foreach (var a in cot) {       var ea = a as IEnumerable&lt;object&gt;;       if (ea==null) yield return a;       else {         foreach (var b in ea.Flat()) yield return b;       }     }     yield break;   } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043c\u043e\u043b\u0447\u0430:  <\/p>\n<pre><code>  public static void Execute(this IEnumerable&lt;object&gt; cot) {     foreach (var a in cot.Flat()) { }   } <\/code><\/pre>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c:  <\/p>\n<pre><code>  public class ValueOf&lt;T&gt; {     public T Value;   }   public static IEnumerable&lt;object&gt; Timeout(this IEnumerable&lt;object&gt; cot,      TimeSpan duration, ValueOf&lt;bool&gt; timeout) {     var limit = DateTimeOffset.Now+duration;     foreach (var a in cot.Flat()) {       if (DateTimeOffset.Now&gt;limit) {         timeout.Value = true;         break;       }       yield return a;     }     yield break;   } <\/code><\/pre>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0440\u0435\u0436\u0435\/\u0447\u0430\u0449\u0435:  <\/p>\n<pre><code>  public static IEnumerable&lt;object&gt; Rate(this IEnumerable&lt;object&gt; cot, double rate) {     double summ = 0.001;     foreach (var a in cot.Flat()) {       summ += 1.0;       while (summ&gt;rate) { summ -= rate; yield return a; }     }     yield break;   } <\/code><\/pre>\n<p>  \u0416\u0434\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f:  <\/p>\n<pre><code>  public static IEnumerable&lt;object&gt; Wait(Func&lt;bool&gt; condition) {     while (!condition()) yield return null;     yield break;   } <\/code><\/pre>\n<p>  \u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u0433\u043e \u0432\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b,  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0441\u0442\u043e\u043b\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d.<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>  public class TimerThread {     bool _IsCancelled, _IsCompleted;     DispatcherTimer Timer;     IEnumerator&lt;object&gt; Enumerator;     public event Action&lt;ProgressInfo&gt; Progress;      public TimerThread(IEnumerable&lt;object&gt; cot, double interval) {       Enumerator = cot.Flat().GetEnumerator();       Timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(interval) };       Timer.Tick += Timer_Tick;     }     void Timer_Tick(object sender, EventArgs ea) {       if (!Enumerator.MoveNext()) {         _IsCompleted = true;         Timer.IsEnabled = false;       } else if (Enumerator.Current is ProgressInfo) {         if (Progress!=null) Progress((ProgressInfo)Enumerator.Current);       }     }     public bool IsEnabled {       get { return Timer.IsEnabled; }       set {         if (_IsCancelled || _IsCompleted) return;         Timer.IsEnabled = value;       }     }     public bool IsCancelled {       get { return _IsCancelled; }       set {         if (!value) return;         _IsCancelled = true;         Timer.IsEnabled = false;       }     }     public bool IsCompleted { get { return _IsCompleted; } }   } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/315280\/\"> https:\/\/habrahabr.ru\/post\/315280\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 <a href=\"https:\/\/habrahabr.ru\/post\/313824\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043f\u0440\u0438\u0432\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u0439\u0441\u044f \u043f\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044e \u043e\u0442 <b>DispatcherTimer<\/b>. \u041c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u0438\u0434\u0435\u044e \u0434\u0430\u043b\u044c\u0448\u0435. \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c\u0438 \u0442\u0430\u0439\u043c\u0435\u0440\u0430\u043c\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0441 \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e. \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-280959","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/280959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=280959"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/280959\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=280959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=280959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=280959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}