是Delegate.EndInvoke()真的有必要吗? [英] Is Delegate.EndInvoke() really necessary?

查看:323
本文介绍了是Delegate.EndInvoke()真的有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几个论坛,甚至一个计算器两个问题说Delegate.EndInvoke使用Delegate.BeginInvoke的时候是必要的。许多我读过谈论使用的BeginInvoke的文章都没有提到使用EndInvoke会。另外,我只用BeginInvoke的部署生产code和有没有出现任何内存问题。我使用的BeginInvoke的方式通常是,我不关心,当他们完成或者他们需要处理多长时间线程。

I've read a couple of forums and even a stackoverflow question or two saying that Delegate.EndInvoke is necessary when using Delegate.BeginInvoke. Many of the articles I've read talking about using BeginInvoke have failed to mention using EndInvoke. Also I've deployed production code using only BeginInvoke and there doesn't appear to be any memory issues. The way I've used BeginInvoke is generally with threads that I don't care about when they finish or how long they take to process.

推荐答案

从MSDN文章呼唤同步异步方法

没有你使用哪种技术的事情,   总是调用EndInvoke来完成你的   异步调用。

No matter which technique you use, always call EndInvoke to complete your asynchronous call.

现在,有理论,再有就是实践。你已经发现,像许多其他开发商之前,你经常可以逃脱忽略此记录的需求。这可能是一个实现细节是否 EndInvoke会实际上做任何事情是绝对必要的prevent崩溃,内存泄露等你的应用程序,但这里的东西:如果它是一个记录的需求,你真的应该去做。这不只是理论;还要保护自己的变化的情况下。

Now, there is theory and then there is practice. You have found, like many other developers before you, that you can often get away with ignoring this documented requirement. It may be an implementation detail whether EndInvoke actually does anything that's absolutely necessary to prevent your application from crashing, leaking memory, etc. But here's the thing: if it's a documented requirement, you really ought to do it. This is not just about theory; it's about protecting yourself in the event of change.

通过记录这一要求,此异步调用机制的设计者基本放弃自己的自由,改变了的BeginInvoke EndInvoke会工作的路线,这样,如果有足够的理由(例如,性能增强), EndInvoke会会突然成为了很多更加必要。假设它会突然导致死锁,如果你忘了。他们已经介绍自己说的随时拨打 EndInvoke会 的;如果您的应用程序停止工作,因为你没有按照这一要求,责任在你。

By documenting this requirement, the designers of this asynchronous calling mechanism basically gave themselves the freedom to change the way BeginInvoke and EndInvoke work down the line so that, if there were sufficient reason (e.g., a performance enhancement), EndInvoke could suddenly become a lot more necessary. Suppose it would suddenly result in a deadlock if you forgot it. They've already covered themselves by saying always call EndInvoke; if your app stops working because you didn't follow this requirement, the onus is on you.

我并不是说这必然是一个可能的情况。我的观点很简单,你shouldn't,或至少的我不会的-ask这真的是必要的吗?用的的心态。如果我可以逃脱留出来,那么我会的,因为它的记载,你应该这样做。

I'm not saying this is necessarily a likely scenario. My point is simply that you shouldn't—or at least I wouldn't—ask "Is this really necessary?" with the mindset of If I can get away with leaving it out, then I will, since it is documented that you should do it.

这篇关于是Delegate.EndInvoke()真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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