是什么SynchronizationContext.Send和SynchronizationContext.Post之间的区别? [英] What is the difference between SynchronizationContext.Send and SynchronizationContext.Post?

查看:799
本文介绍了是什么SynchronizationContext.Send和SynchronizationContext.Post之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于杰里米米勒在函数式编程的日常.NET开发,我有工作命令执行,做一切我希望它(做繁重的线程池,发送结果或错误回到同步上下文,甚至张贴进步回同步上下文),但我无法解释为什么它使用 SynchronizationContext.Send 从线程池和的Synchronization.Post 函数功能传递到了做繁重的方法。我已阅读文档,好几次,但我就是不能得到有什么区别肠道感。什么我应该在的事实,一个被称为获得发送和一个叫发表?我感到神奇的是一个事实,发送启动同步请求和发表 启动一个同步请求,但两者的请求来自线程池,需要发送/调回UI线程。



有人可以解释的差异,哪怕它只是一个记忆装置,让我知道什么时候选择了另一种?



在情况下,重要的,这是我的< STRONG>测试代码在这里我使用发表送进度回UI:

 私人诉讼_ExecuteCommand 
(SynchronizationContext的背景下
,动作< INT,INT>进步
,动作< INT,INT>在)
{
诠释计数= 3;
INT accumulatedValue = 0;
INT的threadId = Thread.CurrentThread.ManagedThreadId;
的for(int i = 0; I<计数;我++)
{
Thread.sleep代码(1000);
context.Post(代表{前进(I + 1的threadId);});
accumulatedValue + =我;
}

回报率()=>后(的threadId,accumulatedValue);
}

_ExecuteCommand 方法传递中作为命令参数下,大多是从原来的文章中,使用发送发送完成和错误信息回到UI:

 公共无效执行(Func键<作用>命令,动作<异常>错误)
{
ThreadPool.QueueUserWorkItem(O =>
{

{
继续行动命令=();
_Context.Send(S = GT;延续());
}
赶上(例外五)
{
_Context.Send(S = GT;错误(E));
}
});
}


解决方案

发送 - 同步:等待答案(或动作完成)



邮政 - 异步:下车并继续



所以,你的例子使用了正确的在正确的时间的方法。没有必要停止for循环,直到进度更新完成(相反)。结果
,并执行不希望等待操作完成,否则,异常处理没有目的。


Thanks to Jeremy Miller's good work in Functional Programming For Everyday .NET Development, I have a working command executor that does everything I want it to (do heavy lifting on the thread pool, send results or errors back to the synchronization context, and even post progress back to the synchronization context), but I can't explain why it uses SynchronizationContext.Send from the thread-pool and Synchronization.Post from the Func passed into the method that does the heavy lifting. I have read the documentation, several times, but I just can't get a gut sense for what the difference is. What am I supposed to get from the fact that one is called Send and one is called Post? I sense the magic is in the fact Send "starts a synchronous request" and Post "starts an asynchronous request", but both requests come from the thread pool and need to be sent/posted back to the UI thread.

Can someone explain the difference, even if it is just a mnemonic device that lets me know when to choose one over the other?

In case it matters, this is my test code where I use Post to send progress back to the UI:

private Action _ExecuteCommand
                (SynchronizationContext context
                 , Action<int, int> progress
                 , Action<int, int> after)
{
    int count = 3;
    int accumulatedValue = 0;
    int threadId = Thread.CurrentThread.ManagedThreadId;
    for (int i = 0; i < count; i++)
    {
        Thread.Sleep(1000);
        context.Post(delegate { progress(i + 1, threadId); });
        accumulatedValue += i;
    }

    return () => after(threadId, accumulatedValue);
}

That _ExecuteCommand method is passed in as the command parameter below, mostly from the original article, that uses Send to send completion and error message back to the UI:

public void Execute(Func<Action> command, Action<Exception> error)
{
    ThreadPool.QueueUserWorkItem(o =>
     {
         try
         {
             Action continuation = command();
             _Context.Send(s => continuation());
         }
         catch (Exception e)
         {
             _Context.Send(s => error(e));
         }
     });
}

解决方案

Send - synchronous: wait for answer (or action completed)

Post - asynchronous: drop off and continue

So your example uses the correct methods at the right moments. There is no need to halt the for-loop until the progress update is complete (on the contrary).
And Execute does want to wait for the Action to complete, otherwise the exception handling has no purpose.

这篇关于是什么SynchronizationContext.Send和SynchronizationContext.Post之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆