{"id":232169,"date":"2014-08-15T13:34:03","date_gmt":"2014-08-15T09:34:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=232169"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=232169","title":{"rendered":"<span class=\"post_title\">SynchronizationContext \u2014 \u043a\u043e\u0433\u0434\u0430 MSDN \u043f\u043e\u0434\u0432\u043e\u0434\u0438\u0442<\/span>"},"content":{"rendered":"<div class=\"content html_format\">       \u041d\u0435 \u0437\u043d\u0430\u044e \u043f\u043e\u0447\u0435\u043c\u0443, \u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 .NET Framework, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e. \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f MSDN \u043f\u043e\u0447\u0442\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f SynchronizationContext. \u0414\u043e\u043b\u0436\u0435\u043d \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u044f \u0438 \u0441\u0430\u043c \u043f\u043b\u043e\u0445\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u044f \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u043d\u044f\u043b \u0435\u0433\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u043e\u0447\u044c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c.<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 SynchronizationContext \u0434\u043b\u044f \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0430 \u043a\u043e\u0434\u0430 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439<\/h4>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c SynchronizationContext. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430, t1 \u0438 t2. \u0418 t1 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443, \u0438 \u0432 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442 \u0436\u0435\u043b\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043f\u043e\u0442\u043e\u043a\u0443 t2. \u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0443 t2 SynchronizationContext, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u0432 t1, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 Send \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u043e\u0434\u0430 \u0432 t2. \u041d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u043c\u0430\u0433\u0438\u044e\u2026 \u041e\u0434\u043d\u0430\u043a\u043e \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0437\u043d\u0430\u0442\u044c \u0447\u0442\u043e \u043d\u0435 \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u043d\u0438\u043c SynchronizationContext. \u0422\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0438\u043c\u0435\u0435\u0442 SynchronizationContext, \u044d\u0442\u043e UI \u043f\u043e\u0442\u043e\u043a.<\/p>\n<p>  \u041a\u0442\u043e \u0437\u0430\u0434\u0430\u0435\u0442 SynchronizationContext \u0434\u043b\u044f UI \u043f\u043e\u0442\u043e\u043a\u0430? \u0415\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f? \u0425\u043e\u0440\u043e\u0448\u043e, \u0432\u043e\u0442 \u0432\u0430\u043c \u043e\u0442\u0432\u0435\u0442, \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442 SynchronizationContext \u0432 \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0430. \u041e\u0442\u043a\u0443\u0434\u0430 \u044f \u044d\u0442\u043e \u0443\u0437\u043d\u0430\u043b? \u041d\u0443\u2026 \u044f \u0437\u0430\u043a\u043e\u0434\u0438\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443.<\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u043e\u0439 \u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 SynchronizationContext.Current, \u043f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e. SynchronizationContext.Current \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c SynchronizationContext \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443. \u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u044f\u0441\u043d\u0438\u043c, SynchronizationContext.Current \u043d\u0435 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 AppDomain, \u043d\u043e \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043f\u043e\u0442\u043e\u043a\u0430. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u0434\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b SynchronizationContext \u0432\u044b\u0437\u0432\u0430\u0432 SynchronizationContext.Current. \u0415\u0441\u043b\u0438 \u0432\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 SynchronizationContext, \u043e\u043d \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430 (\u0438 \u043a\u0430\u043a \u044f \u0441\u043a\u0430\u0437\u0430\u043b \u0440\u0430\u043d\u0435\u0435, \u043d\u0435 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043e\u043c\u0435\u043d\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f).<\/p>\n<p>  \u0425\u043e\u0440\u043e\u0448\u043e, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 \u043d\u0430 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u0435\u0442 SynchronizationContext \u0432 \u043d\u0430\u0448 UI \u043f\u043e\u0442\u043e\u043a:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">[STAThread] static void Main() {     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);      \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438     var context = SynchronizationContext.Current;     if (context == null)         MessageBox.Show(&quot;No context for this thread&quot;);     else         MessageBox.Show(&quot;We got a context&quot;);      \/\/ \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u043e\u0440\u043c\u0443     Form1 form = new Form1();      \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0449\u0435 \u0440\u0430\u0437     context = SynchronizationContext.Current;      if (context == null)         MessageBox.Show(&quot;No context for this thread&quot;);     else         MessageBox.Show(&quot;We got a context&quot;);      if (context == null)         MessageBox.Show(&quot;No context for this thread&quot;);      Application.Run(new Form1()); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435 \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0434\u043e \u0443\u0447\u0435\u0441\u0442\u044c:  <\/p>\n<ul>\n<li>\u041f\u0435\u0440\u0432\u044b\u0439 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0447\u0442\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043d\u0435\u0442 SynchronizationContext&#8217;\u0430 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a \u043f\u043e\u0442\u043e\u043a\u0443. \u042d\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e .NET \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0432 \u044d\u0442\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u0438 \u043d\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/li>\n<li>\u0421\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b, \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d. \u0417\u0430 \u044d\u0442\u043e \u043d\u0435\u0441\u0435\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 Form, \u041e\u043d \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0435\u0441\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0435\u0433\u043e \u0437\u0430\u0434\u0430\u0442\u044c. \u0417\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 UI \u043a\u043e\u043d\u0442\u0440\u043e\u043b \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a \u043d\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435 UI \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. \u041f\u043e\u0442\u043e\u043a \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u043a\u043d\u043e, \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u043e\u043a\u043d\u043e\u043c \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/li>\n<\/ul>\n<h4>\u0418 \u0447\u0442\u043e \u043c\u043d\u0435 \u0441 \u044d\u0442\u0438\u043c \u0432\u0441\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0435\u043b\u0430\u0442\u044c?<\/h4>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 UI \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0434\u0430\u043b \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u0434 \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u043c\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0432 UI \u043f\u043e\u0442\u043e\u043a? \u0414\u0430. \u0415\u0441\u043b\u0438 \u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u043e\u0442\u043b\u0438\u0447\u043d\u043e\u043c \u043e\u0442 UI \u043f\u043e\u0442\u043e\u043a\u0430, \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043e\u0437\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0433\u0435\u0440\u043e\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e? \u0412\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 (\u0432 \u0432\u0435\u0440\u0441\u0438\u0438 1.0 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043f\u0430\u0434\u0435\u0442, \u043d\u043e \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 2.0 \u0435\u0441\u0442\u044c \u0436\u0438\u0440\u043d\u044b\u0435 \u0443\u0440\u043e\u0434\u043b\u0438\u0432\u044b\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u044b\u043f\u043b\u044e\u043d\u0435\u0442 \u0432\u0430\u043c \u0432 \u043b\u0438\u0446\u043e).<\/p>\n<p>  \u0421\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e\u0441\u0442\u0438 \u0440\u0430\u0434\u0438, \u044f \u0441\u043a\u0430\u0436\u0443 \u0447\u0442\u043e \u0432\u044b \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. \u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e InvokeRequired(\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0435\u0441\u0442\u044c \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043b\u044e\u0431\u043e\u0433\u043e UI \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0430) \u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u043b\u0438 \u0432\u0430\u043c \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434. \u0415\u0441\u043b\u0438 InvokeRequired \u0432\u0435\u0440\u043d\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 true, \u0442\u043e \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0439\u0442\u0435 Control.Invoke \u0434\u043b\u044f \u043c\u0430\u0440\u0448\u0430\u043b\u0438\u043d\u0433\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a UI. \u041e\u0442\u043b\u0438\u0447\u043d\u043e! \u041d\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u044d\u0442\u043e\u0439 \u0442\u0435\u0445\u043d\u0438\u043a\u043e\u0439. \u0423 \u0432\u0430\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u0432\u0430\u0442\u044c Invoke. \u041d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u0430\u043a\u043e\u0439 UI \u043a\u043e\u043d\u0442\u0440\u043e\u043b \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u043d\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043e\u0434\u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b, \u0432 \u0432\u0430\u0448\u0435\u043c \u043d\u0435 UI \u043f\u043e\u0442\u043e\u043a\u0435, \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043c\u0430\u0440\u0448\u0430\u043b\u0438\u043d\u0433\u0430. <\/p>\n<p>  \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0434\u0438\u0437\u0430\u0439\u043d\u0430, \u0432\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 UI \u0432 \u0431\u0438\u0437\u043d\u0435\u0441-\u0441\u043b\u043e\u0435. \u0422\u043e\u0433\u0434\u0430 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 UI \u043a\u043b\u0430\u0441\u0441\u0443, \u0438 \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c \u0447\u0442\u043e UI \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0441\u0435\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0437\u0430 \u043c\u0430\u0440\u0448\u0430\u043b\u0438\u043d\u0433(\u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043c\u043e\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u043e MVP). \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0435\u0442 UI \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 UI \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u043c \u0447\u0435\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b. \u0411\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e \u043d\u0430 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0440\u0448\u0430\u043b\u0438\u043d\u0433\u0430 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0439\u0444\u0435\u0439\u0441, \u043d\u0435 \u0438\u043c\u0435\u044f \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044b \u0438\u043b\u0438 \u043d\u0430 \u0444\u043e\u0440\u043c\u0443.<\/p>\n<p>  \u0418 \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f?<\/p>\n<p>  \u0414\u0430 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u043e, \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043c\u0430\u0440\u0448\u0430\u043b\u0438\u043d\u0433\u0430 \u0432 UI \u043f\u043e\u0442\u043e\u043a. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440.<br \/>  \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0443 \u043c\u0435\u043d\u044f \u0435\u0441\u0442\u044c listBox \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0438\u0437 \u0440\u0430\u0431\u043e\u0447\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u041f\u043e\u0442\u043e\u043a \u0438\u043c\u0438\u0442\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 listBox. \u041f\u043e\u0442\u043e\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \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 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 mToolStripButtonThreads_Click.<\/p>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0447\u0442\u043e \u043d\u0430 \u0444\u043e\u0440\u043c\u0435:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0447\u0442\u043e \u043d\u0430 \u0444\u043e\u0440\u043c\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private void InitializeComponent()     {         System.ComponentModel.ComponentResourceManager resources =           new System.ComponentModel.ComponentResourceManager(typeof(Form1));         this.mListBox = new System.Windows.Forms.ListBox();         this.toolStrip1 = new System.Windows.Forms.ToolStrip();         this.mToolStripButtonThreads = new System.Windows.Forms.ToolStripButton();         this.toolStrip1.SuspendLayout();         this.SuspendLayout();         \/\/         \/\/ mListBox         \/\/         this.mListBox.Dock = System.Windows.Forms.DockStyle.Fill;         this.mListBox.FormattingEnabled = true;         this.mListBox.Location = new System.Drawing.Point(0, 0);         this.mListBox.Name = &quot;mListBox&quot;;         this.mListBox.Size = new System.Drawing.Size(284, 264);         this.mListBox.TabIndex = 0;         \/\/         \/\/ toolStrip1         \/\/         this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {         this.mToolStripButtonThreads});         this.toolStrip1.Location = new System.Drawing.Point(0, 0);         this.toolStrip1.Name = &quot;toolStrip1&quot;;         this.toolStrip1.Size = new System.Drawing.Size(284, 25);         this.toolStrip1.TabIndex = 1;         this.toolStrip1.Text = &quot;toolStrip1&quot;;         \/\/         \/\/ mToolStripButtonThreads         \/\/         this.mToolStripButtonThreads.DisplayStyle =           System.Windows.Forms.ToolStripItemDisplayStyle.Text;         this.mToolStripButtonThreads.Image = ((System.Drawing.Image)             (resources.GetObject(&quot;mToolStripButtonThreads.Image&quot;)));         this.mToolStripButtonThreads.ImageTransparentColor =              System.Drawing.Color.Magenta;         this.mToolStripButtonThreads.Name = &quot;mToolStripButtonThreads&quot;;         this.mToolStripButtonThreads.Size = new System.Drawing.Size(148, 22);         this.mToolStripButtonThreads.Text = &quot;Press Here to start threads&quot;;         this.mToolStripButtonThreads.Click +=           new System.EventHandler(this.mToolStripButtonThreads_Click);         \/\/         \/\/ Form1         \/\/         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(284, 264);         this.Controls.Add(this.toolStrip1);         this.Controls.Add(this.mListBox);         this.Name = &quot;Form1&quot;;         this.Text = &quot;Form1&quot;;         this.toolStrip1.ResumeLayout(false);         this.toolStrip1.PerformLayout();         this.ResumeLayout(false);         this.PerformLayout();     }      #endregion      private System.Windows.Forms.ListBox mListBox;     private System.Windows.Forms.ToolStrip toolStrip1;     private System.Windows.Forms.ToolStripButton mToolStripButtonThreads; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public partial class Form1 : Form {     public Form1()     {         InitializeComponent();     }      private void mToolStripButtonThreads_Click(object sender, EventArgs e)     {         \/\/ \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c id \u043f\u043e\u0442\u043e\u043a\u0430         int id = Thread.CurrentThread.ManagedThreadId;         Trace.WriteLine(&quot;mToolStripButtonThreads_Click thread: &quot; + id);          \/\/ \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441 \u044d\u0442\u0438\u043c         \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u043c (UI \u043f\u043e\u0442\u043e\u043a), \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0435\u0433\u043e \u0432 uiContext         \/\/ \u043e\u0442\u043c\u0435\u0442\u044c\u0435 \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435         \/\/ \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b (\u0432\u043d\u0435 \u0437\u043e\u043d\u044b \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f)         \/\/ \u0442\u0430\u043a\u0436\u0435 \u043e\u0442\u043c\u0435\u0442\u044c\u0435, \u0447\u0442\u043e \u043d\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u043d\u0438\u043c.         SynchronizationContext uiContext = SynchronizationContext.Current;          \/\/ \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0442\u043e\u043a \u0438 \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u0435\u043c\u0443 \u043c\u0435\u0442\u043e\u0434 Run \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f         Thread thread = new Thread(Run);          \/\/ \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u043e\u0442\u043e\u043a \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438,         \/\/ \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c UI         thread.Start(uiContext);     }      private void Run(object state)     {         \/\/ \u0441\u043c\u043e\u0442\u0440\u0438 id \u043f\u043e\u0442\u043e\u043a\u0430         int id = Thread.CurrentThread.ManagedThreadId;         Trace.WriteLine(&quot;Run thread: &quot; + id);          \/\/ \u0432\u044b\u0442\u0430\u0449\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0437 state'\u0430         SynchronizationContext uiContext = state as SynchronizationContext;          for (int i = 0; i &lt; 1000; i++)         { \t\t\t\/\/ \u0422\u0443\u0442 \u043c\u043e\u0433 \u0431\u044b \u0431\u044b\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0431\u0430\u0437\u0435 \t\t\t\/\/ \u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f             Thread.Sleep(10);              \/\/ \u0438\u0441\u043f\u043e\u043b\u044c\u0443\u0435\u043c UI \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430,  \t\t\t\/\/ \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 UpdateUI, \u043c\u0435\u0442\u043e\u0434 UpdateUI  \t\t\t\/\/ \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435              uiContext.Post(UpdateUI, &quot;line &quot; + i.ToString());         }     }      \/\/\/ &lt;summary&gt;     \/\/\/ \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c UI \u043f\u043e\u0442\u043e\u043a\u0435     \/\/\/ &lt;\/summary&gt;     private void UpdateUI(object state)     {         int id = Thread.CurrentThread.ManagedThreadId;         Trace.WriteLine(&quot;UpdateUI thread:&quot; + id);         string text = state as string;         mListBox.Items.Add(text);     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u043a\u043e\u0434\u0443, \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u044f \u0432\u044b\u0432\u043e\u0436\u0443 id \u043f\u043e\u0442\u043e\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<p>  \u041f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 ToolStrip, \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u043d\u0430 \u043c\u0435\u0442\u043e\u0434 Run. \u0412 \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a \u044f \u043f\u0435\u0440\u0435\u0434\u0430\u044e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 UI \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<pre><code class=\"cs\">SynchronizationContext uiContext = SynchronizationContext.Current; <\/code><\/pre>\n<p>  \u042f \u0437\u043d\u0430\u044e \u0447\u0442\u043e \u0432 SynchronizationContext.Current \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 UI \u043f\u043e\u0442\u043e\u043a\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u043d\u0430\u0436\u0430\u0442\u0438\u044e \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 (UI \u043a\u043e\u043d\u0442\u0440\u043e\u043b). \u041c\u0435\u0442\u043e\u0434 Run \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0430 \u043a\u043e\u0434\u0430 \u0432 UI \u043f\u043e\u0442\u043e\u043a.<\/p>\n<pre><code class=\"cs\">\/\/ \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f SynchronizationContext uiContext = state as SynchronizationContext; <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 Run 1000 \u0440\u0430\u0437 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 listBox. \u041a\u0430\u043a? \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 Send \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"cs\">public virtual void Send(SendOrPostCallback d, object state); <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 Send \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435\u2026  <\/p>\n<pre><code class=\"cs\">uiContext.Send(UpdateUI, &quot;line &quot; + i.ToString()); <\/code><\/pre>\n<p>  &#8230;UpdateUI \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043c\u0435\u0442\u043e\u0434, \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 listBox. \u041a\u043e\u0434 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430 UpdateUI \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435, \u0430 \u043d\u0435 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u0435\u0433\u043e.<\/p>\n<pre><code class=\"cs\">private void UpdateUI(object state) {     int id = Thread.CurrentThread.ManagedThreadId;     Trace.WriteLine(&quot;UpdateUI thread:&quot; + id);     string text = state as string;     mListBox.Items.Add(text); } <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0435\u043d\u0438\u0435 \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043f\u043e\u0442\u043e\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. \u0412 \u043d\u0435\u043c \u043d\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 InvokerRequired, \u0442.\u043a. \u044f \u0437\u043d\u0430\u044e \u0447\u0442\u043e \u044d\u0442\u043e UI \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0431\u044b\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u043c\u0435\u0442\u043e\u0434 Send \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 UI \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 id \u043f\u043e\u0442\u043e\u043a\u043e\u0432:  <\/p>\n<pre><code class=\"markdown\">mToolStripButtonThreads_Click thread: 10 Run thread: 3 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 UpdateUI thread:10 ... (x1000 times) <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e id UI \u043f\u043e\u0442\u043e\u043a\u0430 \u0440\u0430\u0432\u0435\u043d 10, \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u043e\u0442\u043e\u043a (Run) \u0438\u043c\u0435\u0435\u0442 id \u0440\u0430\u0432\u043d\u044b\u0439 \u0442\u0440\u0435\u043c, \u0438 \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \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, id \u043f\u043e\u0442\u043e\u043a\u0430 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0440\u0430\u0432\u043d\u043e 10. \u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0438 \u0440\u0435\u043a\u043b\u0430\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u043e\u0441\u044c.<\/p>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h4>\n<p>  \u041e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e, \u043c\u044b \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0432 UI \u043f\u043e\u0442\u043e\u043a, \u043d\u043e \u0447\u0442\u043e \u0441\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u043a\u0438\u043d\u0443\u0442\u044b\u0439 \u043a\u043e\u0434 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435? \u041a\u0442\u043e \u043d\u0435\u0441\u0435\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0437\u0430 \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442? UI \u043f\u043e\u0442\u043e\u043a \u0438\u043b\u0438 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u043e\u0442\u043e\u043a?<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0431\u0440\u043e\u0441\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private void Run(object state) {     \/\/ \u0441\u043c\u043e\u0442\u0440\u0438\u043c id \u043f\u043e\u0442\u043e\u043a\u0430     int id = Thread.CurrentThread.ManagedThreadId;     Trace.WriteLine(&quot;Run thread: &quot; + id);      \/\/ \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438     SynchronizationContext uiContext = state as SynchronizationContext;      for (int i = 0; i &lt; 1000; i++)     {         Trace.WriteLine(&quot;Loop &quot; + i.ToString());         \/\/ \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439         Thread.Sleep(10);          \/\/ \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u0434 \u0432 UI \u043f\u043e\u0442\u043e\u043a         try         {             uiContext.Send(UpdateUI, &quot;line &quot; + i.ToString());         }         catch (Exception e)         {             Trace.WriteLine(e.Message);         }     } }  \/\/\/ &lt;summary&gt; \/\/\/ \u041c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435 \/\/\/ &lt;\/summary&gt; private void UpdateUI(object state) {     throw new Exception(&quot;Boom&quot;); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u042f \u0438\u0437\u043c\u0435\u043d\u0438\u043b \u043c\u0435\u0442\u043e\u0434 UpdateUI, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0432\u044b\u043a\u0438\u0434\u044b\u0432\u0430\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u043b try\/catch \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u0435 Send \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u041f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u044f \u0443\u0432\u0438\u0434\u0435\u043b \u0447\u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 Run, \u0430 \u043d\u0435 \u0432 UI. \u042d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0442.\u043a. \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044f \u0432\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. <br \/>  \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0435\u0441\u0442\u044c \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 Send \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0438\u0438; \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043d\u0430\u0448 \u043a\u043e\u0434 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u043c \u043b\u044e\u0431\u043e\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0435\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<h4>Send vs. Post<\/h4>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 Send \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u0440\u0438\u043a\u0438\u043d\u0443\u0442\u044c \u043a\u043e\u0434 \u0432 UI \u043f\u043e\u0442\u043e\u043a. \u0412\u0442\u043e\u0440\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 Post. \u0415\u0441\u0442\u044c \u043b\u0438 \u0440\u0430\u0437\u043d\u0438\u0446\u0430? \u041e\u043d\u0430 \u043e\u0433\u0440\u043e\u043c\u043d\u0430!<\/p>\n<p>  \u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 SynchronizationContext.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ISynchronizationContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">\/\/ Summary: \/\/     Provides the basic functionality for propagating a synchronization context \/\/     in various synchronization models. public class SynchronizationContext {     \/\/ Summary:     \/\/     Creates a new instance of the System.Threading.SynchronizationContext class.     public SynchronizationContext();      \/\/ Summary:     \/\/     Gets the synchronization context for the current thread.     \/\/     \/\/ Returns:     \/\/     A System.Threading.SynchronizationContext object representing the current     \/\/     synchronization context.     public static SynchronizationContext Current { get; }      \/\/ Summary:     \/\/     When overridden in a derived class, creates a copy of the synchronization     \/\/     context.     \/\/     \/\/ Returns:     \/\/     A new System.Threading.SynchronizationContext object.     public virtual SynchronizationContext CreateCopy();     \/\/     \/\/ Summary:     \/\/     Determines if wait notification is required.     \/\/     \/\/ Returns:     \/\/     true if wait notification is required; otherwise, false.     public bool IsWaitNotificationRequired();     \/\/     \/\/ Summary:     \/\/     When overridden in a derived class, responds to the notification that an     \/\/     operation has completed.     public virtual void OperationCompleted();     \/\/     \/\/ Summary:     \/\/     When overridden in a derived class, responds to the notification that an     \/\/     operation has started.     public virtual void OperationStarted();     \/\/     \/\/ Summary:     \/\/     When overridden in a derived class, dispatches an asynchronous message to     \/\/     a synchronization context.     \/\/     \/\/ Parameters:     \/\/   d:     \/\/     The System.Threading.SendOrPostCallback delegate to call.     \/\/     \/\/   state:     \/\/     The object passed to the delegate.     public virtual void Post(SendOrPostCallback d, object state);     \/\/     \/\/ Summary:     \/\/     When overridden in a derived class, dispatches a synchronous message to a     \/\/     synchronization context.     \/\/     \/\/ Parameters:     \/\/   d:     \/\/     The System.Threading.SendOrPostCallback delegate to call.     \/\/     \/\/   state:     \/\/     The object passed to the delegate.     public virtual void Send(SendOrPostCallback d, object state);     \/\/     \/\/ Summary:     \/\/     Sets the current synchronization context.     \/\/     \/\/ Parameters:     \/\/   syncContext:     \/\/     The System.Threading.SynchronizationContext object to be set.     public static void SetSynchronizationContext(SynchronizationContext syncContext);     \/\/     \/\/ Summary:     \/\/     Sets notification that wait notification is required and prepares the callback     \/\/     method so it can be called more reliably when a wait occurs.     protected void SetWaitNotificationRequired();     \/\/     \/\/ Summary:     \/\/     Waits for any or all the elements in the specified array to receive a signal.     \/\/     \/\/ Parameters:     \/\/   waitHandles:     \/\/     An array of type System.IntPtr that contains the native operating system     \/\/     handles.     \/\/     \/\/   waitAll:     \/\/     true to wait for all handles; false to wait for any handle.     \/\/     \/\/   millisecondsTimeout:     \/\/     The number of milliseconds to wait, or System.Threading.Timeout.Infinite     \/\/     (-1) to wait indefinitely.     \/\/     \/\/ Returns:     \/\/     The array index of the object that satisfied the wait.     [PrePrepareMethod]     [CLSCompliant(false)]     public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout);     \/\/     \/\/ Summary:     \/\/     Helper function that waits for any or all the elements in the specified array     \/\/     to receive a signal.     \/\/     \/\/ Parameters:     \/\/   waitHandles:     \/\/     An array of type System.IntPtr that contains the native operating system     \/\/     handles.     \/\/     \/\/   waitAll:     \/\/     true to wait for all handles; false to wait for any handle.     \/\/     \/\/   millisecondsTimeout:     \/\/     The number of milliseconds to wait, or System.Threading.Timeout.Infinite     \/\/     (-1) to wait indefinitely.     \/\/     \/\/ Returns:     \/\/     The array index of the object that satisfied the wait.     [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]     [PrePrepareMethod]     [CLSCompliant(false)]     protected static int WaitHelper(IntPtr[] waitHandles,                      bool waitAll, int millisecondsTimeout); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u043a \u043c\u0435\u0442\u043e\u0434\u0443 Post:  <\/p>\n<pre><code class=\"cs\">\/\/ \/\/ Summary: \/\/\t \/\/     When overridden in a derived class, dispatches an asynchronous message to \/\/     a synchronization context. \/\/ \/\/ Parameters: \/\/   d: \/\/     The System.Threading.SendOrPostCallback delegate to call. \/\/ \/\/   state: \/\/     The object passed to the delegate. public virtual void Post(SendOrPostCallback d, object state); <\/code><\/pre>\n<p>  \u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0437\u0434\u0435\u0441\u044c: \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 Post \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0436\u0434\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430, \u0434\u043b\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f. \u00ab\u0412\u044b\u0441\u0442\u0440\u0435\u043b\u0438\u043b \u0438 \u0437\u0430\u0431\u044b\u043b\u00bb \u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u043c \u043a\u043e\u0434\u0435. \u0422\u0430\u043a\u0436\u0435 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0442\u043e \u0432\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 Send. \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 UI \u043f\u043e\u0442\u043e\u043a. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, UI \u043f\u043e\u0442\u043e\u043a \u0443\u043f\u0430\u0434\u0435\u0442.<\/p>\n<p>  \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, Post \u0438\u043b\u0438 Send \u0432\u044b \u0432\u044b\u0431\u0435\u0440\u0435\u0442\u0435, \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0417\u0430\u043c\u0435\u043d\u0438\u0432 Send \u043d\u0430 Post, \u0432\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 UI \u043f\u043e\u0442\u043e\u043a\u0430 \u0432 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u043c \u043a\u043e\u0434\u0435.<\/p>\n<h4>\u0422\u0435\u043f\u0435\u0440\u044c \u044f \u043c\u043e\u0433\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c SynchronizationContext \u0434\u043b\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043b\u044e\u0431\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0432\u0435\u0440\u043d\u043e? \u041d\u0435\u0430!<\/h4>\n<p>  \u0412 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c SynchronizationContext \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0432\u044b \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u0435 \u0447\u0442\u043e \u0432\u0430\u0448 \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 SynchronizationContext.Current \u043f\u043e\u043b\u0443\u0447\u0438\u0442 null. \u041d\u0438\u0447\u0435\u0433\u043e \u0441\u0442\u0440\u0430\u0448\u043d\u043e\u0433\u043e \u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b, \u0438 \u0437\u0430\u0434\u0430\u0434\u0438\u0442\u0435 SynchronizationContext, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u043d\u0435\u0442. \u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u043e. \u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0440\u0430\u043d\u0435\u0435.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">class Program {     private static SynchronizationContext mT1 = null;      static void Main(string[] args)     {         \/\/ \u0437\u0430\u043f\u0438\u0448\u0435\u043c id \u043f\u043e\u0442\u043e\u043a\u0430         int id = Thread.CurrentThread.ManagedThreadId;         Console.WriteLine(&quot;Main thread is &quot; + id);          \/\/ \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430         var context = new SynchronizationContext();         \/\/ \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443         SynchronizationContext.SetSynchronizationContext(context);          \/\/ \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438         Thread t1 = new Thread(new ParameterizedThreadStart(Run1));         t1.Start(SynchronizationContext.Current);         Console.ReadLine();     }      static private void Run1(object state)     {         int id = Thread.CurrentThread.ManagedThreadId;         Console.WriteLine(&quot;Run1 Thread ID: &quot; + id);          \/\/ \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f         var context = state as SynchronizationContext;          \/\/ \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043a\u043e\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438         context.Send(DoWork, null);          while (true)             Thread.Sleep(10000000);     }      static void DoWork(object state)     {         int id = Thread.CurrentThread.ManagedThreadId;         Console.WriteLine(&quot;DoWork Thread ID:&quot; + id);     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043a \u0432\u0430\u043c \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c. \u042d\u0442\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u044f \u0437\u0430\u0434\u0430\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0433\u043b\u0430\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u042f \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u044e \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440. \u0418 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e \u0435\u0433\u043e \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 UI \u043f\u043e\u0442\u043e\u043a \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0444\u043e\u0440\u043c\u0430 (\u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c, \u043e\u0431\u044a\u044f\u0441\u043d\u044e \u043f\u043e\u0437\u0436\u0435). \u0417\u0430\u0442\u0435\u043c \u044f \u0441\u043e\u0437\u0434\u0430\u044e \u043f\u043e\u0442\u043e\u043a Run1, \u0438 \u043f\u043e\u0441\u044b\u043b\u0430\u044e \u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u041a\u043e\u0433\u0434\u0430 \u044f \u043f\u044b\u0442\u0430\u044e\u0441\u044c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Send, \u0442\u043e \u0433\u043b\u044f\u0434\u044f \u043d\u0430 \u0432\u044b\u0432\u043e\u0434, \u044f \u0432\u0438\u0436\u0443 \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 Run1, \u0430 \u043d\u0435 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043a\u0430\u043a \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c. \u0412\u043e\u0442 \u0432\u044b\u0432\u043e\u0434:  <\/p>\n<pre><code class=\"markdown\">Main thread is 10 Run1 Thread ID: 11 DoWork Thread ID:11 <\/code><\/pre>\n<p>  \u0412\u0438\u0434\u0438\u0442\u0435, DoWork \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u0436\u0435 \u043f\u043e\u0442\u043e\u043a\u0435 \u0447\u0442\u043e \u0438 Run1. \u0410 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u041f\u043e\u0447\u0435\u043c\u0443? \u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<br \/>  \u041d\u0443\u2026 \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0432\u044b \u043f\u043e\u0439\u043c\u0435\u0442\u0435 \u0447\u0442\u043e \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0433\u043e \u0432 \u044d\u0442\u043e\u0439 \u0436\u0438\u0437\u043d\u0438. \u041f\u043e\u0442\u043e\u043a\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438, \u0438\u043c \u043d\u0443\u0436\u043d\u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0432 \u043d\u0438\u0445 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041f\u043e\u0442\u043e\u043a UI, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u0438 \u0432 \u0441\u0432\u043e\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u044d\u0442\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043b\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435.<\/p>\n<p>  \u0422.\u0435. UI \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0439 \u043e\u0442 SynchronizationContext, \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u043d System.Windows.Forms.WindowsFormsSynchronizationContext. \u0418 \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0438\u043c\u0435\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 SynchronizationContext. \u0412\u0435\u0440\u0441\u0438\u044f UI \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u0432 Send \u0438 Post, \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 (\u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u043e \u043d\u0435 \u043d\u0430\u0448\u0435\u043b). \u0427\u0442\u043e \u0436\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SynchronizationContext?<\/p>\n<p>  \/*<\/p>\n<p>  <b><u>\u043e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/u><\/b><\/p>\n<p>  <a href=\"http:\/\/referencesource.microsoft.com\/#System.Windows.Forms\/ndp\/fx\/src\/winforms\/Managed\/System\/WinForms\/WindowsFormsSynchronizationContext.cs\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 WindowsFormsSynchronizationContext <\/a><br \/>  <a href=\"http:\/\/referencesource.microsoft.com\/#mscorlib\/system\/threading\/synchronizationcontext.cs\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 SynchronizationContext<\/a><\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f InvokeRequired \u0432 WindowsFormsSynchronizationContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public bool InvokeRequired  {     get  \t{          using (new MultithreadSafeCallScope())         {             HandleRef hwnd;             if (IsHandleCreated) {                 hwnd = new HandleRef(this, Handle);             }             else {                 Control marshalingControl = FindMarshalingControl();                  if (!marshalingControl.IsHandleCreated) {                     return false;                 }                  hwnd = new HandleRef(marshalingControl, marshalingControl.Handle);             }              int pid;             int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(hwnd, out pid);             int currentThread = SafeNativeMethods.GetCurrentThreadId();             return(hwndThread != currentThread);         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Invoke \u0432 WindowsFormsSynchronizationContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private Object MarshaledInvoke(Control caller, Delegate method, Object[] args, bool synchronous)  {   \/\/ Marshaling an invoke occurs in three steps:   \/\/   \/\/ 1.  Create a ThreadMethodEntry that contains the packet of information   \/\/     about this invoke.  This TME is placed on a linked list of entries because   \/\/     we have a gap between the time we PostMessage and the time it actually   \/\/     gets processed, and this gap may allow other invokes to come in.  Access   \/\/     to this linked list is always synchronized.   \/\/   \/\/ 2.  Post ourselves a message.  Our caller has already determined the   \/\/     best control to call us on, and we should almost always have a handle.   \/\/   \/\/ 3.  If we're synchronous, wait for the message to get processed.  We don't do   \/\/     a SendMessage here so we're compatible with OLE, which will abort many   \/\/     types of calls if we're within a SendMessage.   \/\/    if (!IsHandleCreated) {       throw new InvalidOperationException(SR.GetString(SR.ErrorNoMarshalingThread));   }    \/\/ We have to demand unmanaged code permission here for the control hosted in   \/\/ the browser case. Without this check, we will expose a security hole, because   \/\/ ActiveXImpl.OnMessage() will assert unmanaged code for everyone as part of   \/\/ its implementation.   \/\/ The right fix is to remove the Assert() on top of the ActiveXImpl class, and   \/\/ visit each method to see if it needs unmanaged code permission, and if so, add   \/\/ the permission just to that method(s).   \/\/   ActiveXImpl activeXImpl = (ActiveXImpl)Properties.GetObject(PropActiveXImpl);   if (activeXImpl != null) {       IntSecurity.UnmanagedCode.Demand();   }    \/\/ We don't want to wait if we're on the same thread, or else we'll deadlock.   \/\/ It is important that syncSameThread always be false for asynchronous calls.   \/\/   bool syncSameThread = false;   int pid; \/\/ ignored   if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, Handle), out pid) == SafeNativeMethods.GetCurrentThreadId()) {       if (synchronous)           syncSameThread = true;   }    \/\/ Store the compressed stack information from the thread that is calling the Invoke()   \/\/ so we can assign the same security context to the thread that will actually execute   \/\/ the delegate being passed.   \/\/   ExecutionContext executionContext = null;   if (!syncSameThread) {       executionContext = ExecutionContext.Capture();   }   ThreadMethodEntry tme = new ThreadMethodEntry(caller, this, method, args, synchronous, executionContext);    lock (this) {       if (threadCallbackList == null) {           threadCallbackList = new Queue();       }   }    lock (threadCallbackList) {       if (threadCallbackMessage == 0) {           threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + &quot;_ThreadCallbackMessage&quot;);       }       threadCallbackList.Enqueue(tme);   }    if (syncSameThread) {       InvokeMarshaledCallbacks();   }  else {       \/\/         UnsafeNativeMethods.PostMessage(new HandleRef(this, Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);   }    if (synchronous) {       if (!tme.IsCompleted) {           WaitForWaitHandle(tme.AsyncWaitHandle);       }       if (tme.exception != null) {           throw tme.exception;       }       return tme.retVal;   }   else {       return(IAsyncResult)tme;   } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  *\/<\/p>\n<p>  \u0422\u0430\u043a \u0438\u043b\u0438 \u0438\u043d\u0430\u0447\u0435 \u044f \u043d\u0430\u0448\u0435\u043b \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 SynchronizationContext, \u0432\u043e\u0442 \u043e\u043d (\u044f \u0443\u0434\u0430\u043b\u0438\u043b \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0438 \u0441\u0434\u0435\u043b\u0430\u043b \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435):  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SynchronizationContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace System.Threading {     using Microsoft.Win32.SafeHandles;     using System.Security.Permissions;     using System.Runtime.InteropServices;     using System.Runtime.CompilerServices;     using System.Runtime.ConstrainedExecution;     using System.Reflection;      internal struct SynchronizationContextSwitcher : IDisposable     {         internal SynchronizationContext savedSC;         internal SynchronizationContext currSC;         internal ExecutionContext _ec;          public override bool Equals(Object obj)         {             if (obj == null || !(obj is SynchronizationContextSwitcher))                 return false;             SynchronizationContextSwitcher sw = (SynchronizationContextSwitcher)obj;             return (this.savedSC == sw.savedSC &&                     this.currSC == sw.currSC && this._ec == sw._ec);         }          public override int GetHashCode()         {             return ToString().GetHashCode();         }          public static bool operator ==(SynchronizationContextSwitcher c1,                                        SynchronizationContextSwitcher c2)         {             return c1.Equals(c2);         }          public static bool operator !=(SynchronizationContextSwitcher c1,                                        SynchronizationContextSwitcher c2)         {             return !c1.Equals(c2);         }          void IDisposable.Dispose()         {             Undo();         }          internal bool UndoNoThrow()         {             if (_ec  == null)             {                 return true;             }              try             {                 Undo();             }             catch             {                 return false;             }             return true;         }          public void Undo()         {             if (_ec  == null)             {                 return;             }              ExecutionContext  executionContext =               Thread.CurrentThread.GetExecutionContextNoCreate();             if (_ec != executionContext)             {                 throw new InvalidOperationException(Environment.GetResourceString(                           &quot;InvalidOperation_SwitcherCtxMismatch&quot;));             }             if (currSC != _ec.SynchronizationContext)             {                 throw new InvalidOperationException(Environment.GetResourceString(                           &quot;InvalidOperation_SwitcherCtxMismatch&quot;));             }             BCLDebug.Assert(executionContext != null, &quot; ExecutionContext can't be null&quot;);             \/\/ restore the Saved Sync context as current             executionContext.SynchronizationContext = savedSC;             \/\/ can't reuse this anymore             _ec = null;         }     }      public delegate void SendOrPostCallback(Object state);      [Flags]     enum SynchronizationContextProperties     {         None = 0,         RequireWaitNotification = 0x1     };      public class SynchronizationContext     {         SynchronizationContextProperties _props = SynchronizationContextProperties.None;          public SynchronizationContext()         {         }          \/\/ protected so that only the derived sync         \/\/ context class can enable these flags         protected void SetWaitNotificationRequired()         {             \/\/ Prepare the method so that it can be called             \/\/ in a reliable fashion when a wait is needed.             \/\/ This will obviously only make the Wait reliable             \/\/ if the Wait method is itself reliable. The only thing             \/\/ preparing the method here does is to ensure there             \/\/ is no failure point before the method execution begins.              RuntimeHelpers.PrepareDelegate(new WaitDelegate(this.Wait));             _props |= SynchronizationContextProperties.RequireWaitNotification;         }          public bool IsWaitNotificationRequired()         {             return ((_props &               SynchronizationContextProperties.RequireWaitNotification) != 0);         }          public virtual void Send(SendOrPostCallback d, Object state)         {             d(state);         }          public virtual void Post(SendOrPostCallback d, Object state)         {             ThreadPool.QueueUserWorkItem(new WaitCallback(d), state);         }          public virtual void OperationStarted()         {         }          public virtual void OperationCompleted()         {         }          \/\/ Method called when the CLR does a wait operation         public virtual int Wait(IntPtr[] waitHandles,                        bool waitAll, int millisecondsTimeout)         {             return WaitHelper(waitHandles, waitAll, millisecondsTimeout);         }          \/\/ Static helper to which the above method         \/\/ can delegate to in order to get the default         \/\/ COM behavior.         protected static extern int WaitHelper(IntPtr[] waitHandles,                          bool waitAll, int millisecondsTimeout);          \/\/ set SynchronizationContext on the current thread         public static void SetSynchronizationContext(SynchronizationContext syncContext)         {             SetSynchronizationContext(syncContext,               Thread.CurrentThread.ExecutionContext.SynchronizationContext);         }          internal static SynchronizationContextSwitcher           SetSynchronizationContext(SynchronizationContext syncContext,           SynchronizationContext prevSyncContext)         {             \/\/ get current execution context             ExecutionContext ec = Thread.CurrentThread.ExecutionContext;             \/\/ create a switcher             SynchronizationContextSwitcher scsw = new SynchronizationContextSwitcher();              RuntimeHelpers.PrepareConstrainedRegions();             try             {                 \/\/ attach the switcher to the exec context                 scsw._ec = ec;                 \/\/ save the current sync context using the passed in value                 scsw.savedSC = prevSyncContext;                 \/\/ save the new sync context also                 scsw.currSC = syncContext;                 \/\/ update the current sync context to the new context                 ec.SynchronizationContext = syncContext;             }             catch             {                 \/\/ Any exception means we just restore the old SyncCtx                 scsw.UndoNoThrow(); \/\/No exception will be thrown in this Undo()                 throw;             }             \/\/ return switcher             return scsw;         }          \/\/ Get the current SynchronizationContext on the current thread         public static SynchronizationContext Current         {             get             {                 ExecutionContext ec = Thread.CurrentThread.GetExecutionContextNoCreate();                 if (ec != null)                     return ec.SynchronizationContext;                 return null;             }         }          \/\/ helper to Clone this SynchronizationContext,         public virtual SynchronizationContext CreateCopy()         {             \/\/ the CLR dummy has an empty clone function - no member data             return new SynchronizationContext();         }          private static int InvokeWaitMethodHelper(SynchronizationContext syncContext,             IntPtr[] waitHandles,             bool waitAll,             int millisecondsTimeout)         {             return syncContext.Wait(waitHandles, waitAll, millisecondsTimeout);         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 Send \u0438 Post\u2026  <\/p>\n<pre><code class=\"cs\">public virtual void Send(SendOrPostCallback d, Object state) {     d(state); }  public virtual void Post(SendOrPostCallback d, Object state) {     ThreadPool.QueueUserWorkItem(new WaitCallback(d), state); } <\/code><\/pre>\n<p>  Send \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0435 (\u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u0434\u0435\u043b\u0430\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432). Post \u0434\u0435\u043b\u0430\u0435\u0442 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438. \u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c. \u0422\u0430\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0443\u0442\u044b\u0432\u0430\u0435\u0442 \u0438, \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u0430. \u042d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u0440\u0438\u0447\u0438\u043d \u0441\u043f\u043e\u0441\u043e\u0431\u0441\u0442\u0432\u043e\u0432\u0430\u0432\u0448\u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h4>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0432\u044b \u043f\u043e\u0447\u0435\u0440\u043f\u043d\u0443\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u043e\u0432\u043e\u0435 \u0434\u043b\u044f \u0441\u0435\u0431\u044f, \u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u0445 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 .NET \u044f \u043d\u0430\u0448\u0435\u043b \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0445 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \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, \u043e\u0434\u0438\u043d \u0434\u043b\u044f WinForms \u0438 \u043e\u0434\u0438\u043d \u0434\u043b\u044f WPF. \u042f \u0443\u0432\u0435\u0440\u0435\u043d \u0447\u0442\u043e \u0438\u0445 \u0431\u043e\u043b\u044c\u0448\u0435, \u043d\u043e \u043f\u043e\u043a\u0430 \u043d\u0430\u0448\u0435\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0445. \u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043a\u0430\u043a \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432. UI \u043f\u043e\u0442\u043e\u043a, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 Windows API (SendMessage \u0438 PostMessage), \u0442\u0430\u043a \u0447\u0442\u043e \u044f \u0443\u0432\u0435\u0440\u0435\u043d \u0447\u0442\u043e \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<p>  \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0435\u0434\u0435\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e SynchronizationContext, \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0438 \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0442\u0430\u043a\u0443\u044e. \u0412 \u043c\u043e\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b COM, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u0432 STA \u043f\u043e\u0442\u043e\u043a\u0435. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 WCF, \u0438 \u0431\u044b\u043b\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u0440\u043e\u0441 \u043a\u043e\u0434\u0430 \u0432 STA \u043f\u043e\u0442\u043e\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0432\u0435\u0440\u0441\u0438\u044e SynchronizationContext, \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c StaSynchronizationContext, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438 \u043f\u043e\u0439\u0434\u0435\u0442 \u0440\u0435\u0447\u044c \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h5>\u041e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430<\/h5>\n<p>  \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b \u043c\u0435\u043d\u044f \u043a\u043e\u0433\u0434\u0430 \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0442.\u0435. \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435  <\/p>\n<pre><code class=\"cs\">using(var processor = new Processor&lt;int&gt;(handler, exceptionHandler, completedHandler)) {     for(int i=0;i&lt;1000000; i++)         processor.Push(i); } <\/code><\/pre>\n<p>  , \u0438 \u043f\u0435\u0440\u0432\u043e\u0435 \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u044d\u0442\u043e \u0442\u043e \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e. \u041d\u043e \u043f\u043e\u0441\u043b\u0435 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043b\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0447\u0442\u043e SynchronizationContext \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0438 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u043b\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 UI, \u0438 \u044d\u0442\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0441\u0430\u043c\u043e\u043c FCL \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043e\u0442 \u043d\u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f, \u043e\u0434\u0438\u043d \u0434\u043b\u044f WPF \u0438 \u0432\u0442\u043e\u0440\u043e\u0439 \u0434\u043b\u044f WinForms.<\/p>\n<p>  \u041f\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443 \u0432\u0441\u044e \u044d\u0442\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0435\u0434\u0435\u0442\u0441\u044f \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430, \u043d\u043e \u0441\u0430\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0434\u0430\u043d\u044b \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0421\u0430\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u0438\u0445 \u043a\u043e\u043c\u0430\u043d\u0434, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0441\u0435 \u043f\u0438\u0448\u0443\u0442, \u0438 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0434\u0438\u043d \u0447\u0438\u0442\u0430\u0435\u0442.<\/p>\n<p>  \u0422.\u0435. \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u0435\u0435, \u043d\u043e \u043a\u0430\u043a \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0410 \u0438\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0443\u0437\u043a\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u043d\u0430 UI. \u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0436\u0435 \u0441\u0432\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438, 80% \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f. \u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0434\u043b\u044f \u0441\u0432\u043e\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c TPL \u0438\u043b\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (\u0447\u0442\u043e \u043f\u043e \u0438\u0442\u043e\u0433\u0443 \u0438 \u0431\u044b\u043b\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e). \u041d\u0443 \u0438\u043b\u0438 \u043a\u0430\u043a \u0443 \u0430\u0432\u0442\u043e\u0440\u0430 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445.<\/p>\n<p>  \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432 \u0440\u044f\u0434\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u044b SynchronizationContext \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u0435\u0441\u044c\u043c\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432\u044b\u0448\u0435, \u043f\u0440\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 UI&#8217;\u0435\u043c \u0438\u0437 \u0431\u0438\u0437\u043d\u0435\u0441-\u0441\u043b\u043e\u044f, \u0431\u0435\u0437 \u0437\u0430\u0433\u0440\u044f\u0437\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 \u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 BeginInvoke.   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/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\/232169\/\"> http:\/\/habrahabr.ru\/post\/232169\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">       \u041d\u0435 \u0437\u043d\u0430\u044e \u043f\u043e\u0447\u0435\u043c\u0443, \u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 .NET Framework, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e. \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f MSDN \u043f\u043e\u0447\u0442\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f SynchronizationContext. \u0414\u043e\u043b\u0436\u0435\u043d \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u044f \u0438 \u0441\u0430\u043c \u043f\u043b\u043e\u0445\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u044f \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u043d\u044f\u043b \u0435\u0433\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u043e\u0447\u044c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c.<\/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-232169","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/232169","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=232169"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/232169\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}