{"id":267302,"date":"2015-11-02T14:41:03","date_gmt":"2015-11-02T11:41:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=267302"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=267302","title":{"rendered":"\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 SynchronizationContext"},"content":{"rendered":"<p>       \u041d\u0430\u0447\u0430\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u0430\u0432\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 UI \u043d\u0435 \u0438\u0437 UI-\u043f\u043e\u0442\u043e\u043a\u0430. \u0418 \u043a\u043e\u0433\u0434\u0430 \u044f \u043d\u0430\u0447\u0430\u043b \u043b\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u201c\u0433\u043b\u044e\u043a\u0438\u201d, \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e. \u041f\u043e\u0437\u0434\u043d\u0435\u0435 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0432 \u0434\u043e\u0442\u043d\u0435\u0442 \u043c\u0438\u0440\u0435 \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0441\u044f \u0441 SynchronizationContext. \u041d\u043e \u0442\u043e\u0433\u0434\u0430, \u043f\u043e\u0447\u0438\u0442\u0430\u0432 \u043f\u0440\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u044f \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043b, \u0447\u0442\u043e \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u043c\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0434\u0435\u0441\u044c: <a href=\"http:\/\/habrahabr.ru\/post\/232169\/\">SynchronizationContext \u2014 \u043a\u043e\u0433\u0434\u0430 MSDN \u043f\u043e\u0434\u0432\u043e\u0434\u0438\u0442<\/a>.<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043b \u043f\u0440\u043e SynchronizationContext \u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0432\u044b\u0445\u043e\u0434\u043e\u043c c# 5 \u0438 \u0435\u0433\u043e async\/await, \u0442.\u043a. \u044d\u0442\u043e\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0437 \u0441 \u044d\u0442\u0438\u043c \u0441\u0430\u043c\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043b\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0434 \u043c\u043e\u0433 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u0435, \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 UI. \u041d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u044d\u0442\u043e\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u0434 \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043b\u044e\u0431\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u043c:<\/p>\n<pre><code class=\"cs\">Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); await Task.Run(() =&gt; Debug.WriteLine(Thread.CurrentThread.ManagedThreadId)); Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); <\/code><\/pre>\n<p>  \u041c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043a\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435. \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0434\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435 (\u043a\u0440\u043e\u043c\u0435 wcf \u0438 \u0442.\u0434.). \u0412 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043c\u044b\u0441\u043b\u044c, \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0430\u0441 \u0436\u0434\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SynchronizationContext \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u043e\u0434 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u043b\u0438 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u0437 \u043f\u0443\u043b\u0430. \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0436\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c, \u043a\u0430\u043a \u0431\u044b \u043e\u043d\u043e \u043c\u043e\u0433\u043b\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0430 \u0434\u0435\u043b\u0435. \u041e\u0431 \u044d\u0442\u043e\u043c \u0438 \u043f\u043e\u0439\u0434\u0435\u0442 \u0440\u0435\u0447\u044c \u043d\u0438\u0436\u0435.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0432 SynchronizationContext \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0434\u0432\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 Send (\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435) \u0438 Post (\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442 SynchronizationContext \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">CustomSynchronizationContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">class CustomSynchronizationContext : SynchronizationContext, IDisposable {     private readonly AutoResetEvent _eventReset;     private readonly Queue&lt;KeyValuePair&lt;SendOrPostCallback, object&gt;&gt; _workItems;     private readonly Thread _thread;      public CustomSynchronizationContext()     {         _eventReset = new AutoResetEvent(false);         _workItems = new Queue&lt;KeyValuePair&lt;SendOrPostCallback, object&gt;&gt;();         _thread = new Thread(DoWork);         _thread.Start(this);     }      private void DoWork(object obj)     {         SynchronizationContext.SetSynchronizationContext(obj as SynchronizationContext);          while (true)         {             while (_workItems.Count &gt; 0)             {                 var item = _workItems.Dequeue();                 item.Key(item.Value);             }              _eventReset.Reset();             _eventReset.WaitOne();         }     }      public override void Post(SendOrPostCallback d, object state)     {         _workItems.Enqueue(new KeyValuePair&lt;SendOrPostCallback, object&gt;(d, state));         _eventReset.Set();     }      public void Dispose()     {         _eventReset.Dispose();         _thread.Abort();     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c.<\/p>\n<pre><code class=\"cs\">static void Main(string[] args) {     var syncContext = new CustomSynchronizationContext();     Console.WriteLine(Thread.CurrentThread.ManagedThreadId);     syncContext.Post(o =&gt; Console.WriteLine(Thread.CurrentThread.ManagedThreadId), null); } <\/code><\/pre>\n<p>  \u0418 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0432\u0438\u0434\u0438\u043c \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438. \u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442? \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0438 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0430 \u043d\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u0442\u043e\u043a\u0443. \u0422\u0430\u043a \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e \u043d\u0438\u043a\u0442\u043e \u043a\u0440\u043e\u043c\u0435 \u043d\u0430\u0441 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a. \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0437\u0430\u0432\u043e\u0434\u0438\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u044b \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0412-\u0442\u0440\u0435\u0442\u044c\u0438\u0445, \u201c\u043f\u0440\u0438\u043a\u043e\u0441\u0442\u044b\u043b\u0438\u0432\u0430\u0435\u043c\u201d AutoResetEvent, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u043b\u0441\u044f \u0438 \u043d\u0435 \u0437\u0430\u0446\u0438\u043a\u043b\u0438\u0432\u0430\u043b\u0441\u044f \u0431\u0435\u0437 \u0434\u0435\u043b\u0430. \u041d\u0443 \u0438 IDisposable. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0437\u0434\u0435\u0441\u044c \u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043b\u0438\u043a\u0432\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a. \u0422.\u0435. \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cs\">static void Main(string[] args) {     using (var syncContext = new CustomSynchronizationContext())     {         Console.WriteLine(Thread.CurrentThread.ManagedThreadId);         syncContext.Post(o =&gt; Console.WriteLine(Thread.CurrentThread.ManagedThreadId), null);     } } <\/code><\/pre>\n<p>  \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u043e, \u0447\u0442\u043e \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u043d\u043e \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u0434\u0443\u043c\u0430\u044e, \u0441\u043e\u0439\u0434\u0435\u0442. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c.<\/p>\n<p>  \u0427\u0442\u043e \u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439? \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c.<\/p>\n<pre><code class=\"cs\">static void Main(string[] args) {     var syncContext = new CustomSynchronizationContext();     try     {         syncContext.Post(o =&gt; { throw new Exception(&quot;TestException&quot;); }, null);     }     catch (Exception ex)     {         Console.WriteLine(ex.Message);     } } <\/code><\/pre>\n<p>  \u041e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u043f\u0430\u0434\u0430\u0435\u0442 \u0432 \u043d\u0430\u0448\u0435\u043c \u201c\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u043c\u201d \u043f\u043e\u0442\u043e\u043a\u0435. \u0421\u0430\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0438 \u043c\u0435\u0442\u043e\u0434 Send, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">CustomSynchronizationContext (\u0444\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f)<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">class CustomSynchronizationContext : SynchronizationContext, IDisposable {     private readonly AutoResetEvent _workerResetEvent;     private readonly ConcurrentQueue&lt;WorkItem&gt; _workItems;     private readonly Thread _thread;      public CustomSynchronizationContext()     {         _workerResetEvent = new AutoResetEvent(false);         _workItems = new ConcurrentQueue&lt;WorkItem&gt;();         _thread = new Thread(DoWork);         _thread.Start(this);     }      private void DoWork(object obj)     {         SynchronizationContext.SetSynchronizationContext(obj as SynchronizationContext);          while (true)         {             WorkItem workItem;             while (_workItems.TryDequeue(out workItem))                 workItem.Execute();              _workerResetEvent.Reset();             _workerResetEvent.WaitOne();         }     }      public override void Send(SendOrPostCallback d, object state)     {         if (Thread.CurrentThread == _thread)             d(state);         else         {             using (var resetEvent = new AutoResetEvent(false))             {                 var wiExecutionInfo = new WorkItemExecutionInfo();                 _workItems.Enqueue(new SynchronousWorkItem(d, state, resetEvent, ref wiExecutionInfo));                 _workerResetEvent.Set();                  resetEvent.WaitOne();                 if (wiExecutionInfo.HasException)                     throw wiExecutionInfo.Exception;             }         }     }      public override void Post(SendOrPostCallback d, object state)     {         _workItems.Enqueue(new AsynchronousWorkItem(d, state));         _workerResetEvent.Set();     }      public void Dispose()     {         _workerResetEvent.Dispose();         _thread.Abort();     }      private class WorkItemExecutionInfo     {         public bool HasException =&gt; Exception != null;         public Exception Exception { get; set; }     }      private abstract class WorkItem     {         protected readonly SendOrPostCallback SendOrPostCallback;         protected readonly object State;          protected WorkItem(SendOrPostCallback sendOrPostCallback, object state)         {             SendOrPostCallback = sendOrPostCallback;             State = state;         }          public abstract void Execute();     }      private class SynchronousWorkItem : WorkItem     {         private readonly AutoResetEvent _syncObject;         private readonly WorkItemExecutionInfo _workItemExecutionInfo;          public SynchronousWorkItem(SendOrPostCallback sendOrPostCallback, object state, AutoResetEvent resetEvent,             ref WorkItemExecutionInfo workItemExecutionInfo) : base(sendOrPostCallback, state)         {             if (workItemExecutionInfo == null)                 throw new NullReferenceException(nameof(workItemExecutionInfo));              _syncObject = resetEvent;             _workItemExecutionInfo = workItemExecutionInfo;         }          public override void Execute()         {             try             {                 SendOrPostCallback(State);             }             catch (Exception ex)             {                 _workItemExecutionInfo.Exception = ex;             }             _syncObject.Set();         }     }      private class AsynchronousWorkItem : WorkItem     {         public AsynchronousWorkItem(SendOrPostCallback sendOrPostCallback, object state)             : base(sendOrPostCallback, state)         {         }          public override void Execute()         {             SendOrPostCallback(State);         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0432\u0432\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 WorkItem, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u043e\u0434 (\u0434\u0435\u043b\u0435\u0433\u0430\u0442) \u043d\u0443\u0436\u043d\u044b\u043c \u043d\u0430\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c. \u041e\u0442 \u043d\u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u0435\u0449\u0435 \u0434\u0432\u0430 SynchronousWorkItem \u0438 AsynchronousWorkItem, \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0432 \u0447\u0435\u043c \u0438\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435. \u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 (AutoResetEvent) \u0438 \u043f\u043e\u0433\u043b\u043e\u0449\u0435\u043d\u0438\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0432 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0422\u0435\u043f\u0435\u0440\u044c KeyValuePair&lt;SendOrPostCallback, object&gt; \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0430 WorkItem, \u043d\u0443 \u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u0443\u044e. \u0422\u0430\u043a\u0436\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 Send \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u0434\u0440\u0443\u0433 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u201c\u043d\u0430\u0448\u0438\u043c\u201d, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u0437\u0434\u0435\u0441\u044c \u0436\u0435.<\/p>\n<p>  \u0421\u043d\u043e\u0432\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c.<\/p>\n<pre><code class=\"cs\">static void Main(string[] args) {     var syncContext = new CustomSynchronizationContext();     try     {         syncContext.Send(o =&gt; { throw new Exception(&quot;TestException&quot;); }, null);     }     catch (Exception ex)     {         Console.WriteLine(ex.Message);     } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e. \u041d\u0443 \u0438 \u043f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0441\u044f \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0435 \u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"cs\">static void Main(string[] args) {     var syncContext = new CustomSynchronizationContext();     syncContext.Post(TestAsyncMethod, null); }  async static void TestAsyncMethod(object obj) {     Console.WriteLine(Thread.CurrentThread.ManagedThreadId);     await Task.Run(() =&gt; Console.WriteLine(Thread.CurrentThread.ManagedThreadId));     Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } <\/code><\/pre>\n<p>  \u041c\u043e\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<p>  9<br \/>  10<br \/>  9       <\/p>\n<div class=\"clear\"><\/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=\"http:\/\/habrahabr.ru\/post\/269985\/\"> http:\/\/habrahabr.ru\/post\/269985\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041d\u0430\u0447\u0430\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u0430\u0432\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 UI \u043d\u0435 \u0438\u0437 UI-\u043f\u043e\u0442\u043e\u043a\u0430. \u0418 \u043a\u043e\u0433\u0434\u0430 \u044f \u043d\u0430\u0447\u0430\u043b \u043b\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u201c\u0433\u043b\u044e\u043a\u0438\u201d, \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e. \u041f\u043e\u0437\u0434\u043d\u0435\u0435 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0432 \u0434\u043e\u0442\u043d\u0435\u0442 \u043c\u0438\u0440\u0435 \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0441\u044f \u0441 SynchronizationContext. \u041d\u043e \u0442\u043e\u0433\u0434\u0430, \u043f\u043e\u0447\u0438\u0442\u0430\u0432 \u043f\u0440\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u044f \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043b, \u0447\u0442\u043e \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u043c\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0434\u0435\u0441\u044c: <a href=\"http:\/\/habrahabr.ru\/post\/232169\/\">SynchronizationContext \u2014 \u043a\u043e\u0433\u0434\u0430 MSDN \u043f\u043e\u0434\u0432\u043e\u0434\u0438\u0442<\/a>.<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043b \u043f\u0440\u043e SynchronizationContext \u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0432\u044b\u0445\u043e\u0434\u043e\u043c c# 5 \u0438 \u0435\u0433\u043e async\/await, \u0442.\u043a. \u044d\u0442\u043e\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0437 \u0441 \u044d\u0442\u0438\u043c \u0441\u0430\u043c\u044b\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043b\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0434 \u043c\u043e\u0433 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u0435, \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 UI. \u041d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u044d\u0442\u043e\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u0434 \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043b\u044e\u0431\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u043c:<\/p>\n<pre><code class=\"cs\">Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); await Task.Run(() =&gt; Debug.WriteLine(Thread.CurrentThread.ManagedThreadId)); Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); <\/code><\/pre>\n<p>  \u041c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043a\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435. \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0434\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 UI-\u043f\u043e\u0442\u043e\u043a\u0435 (\u043a\u0440\u043e\u043c\u0435 wcf \u0438 \u0442.\u0434.). \u0412 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043c\u044b\u0441\u043b\u044c, \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0430\u0441 \u0436\u0434\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SynchronizationContext \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u043e\u0434 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u043b\u0438 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u0437 \u043f\u0443\u043b\u0430. \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0436\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c, \u043a\u0430\u043a \u0431\u044b \u043e\u043d\u043e \u043c\u043e\u0433\u043b\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0430 \u0434\u0435\u043b\u0435. \u041e\u0431 \u044d\u0442\u043e\u043c \u0438 \u043f\u043e\u0439\u0434\u0435\u0442 \u0440\u0435\u0447\u044c \u043d\u0438\u0436\u0435.  <\/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-267302","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267302","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=267302"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267302\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}