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

问题描述

我已经阅读了几个论坛,甚至是一个stackoverflow问题,或者两个使用Delegate.BeginInvoke时,Delegate.EndInvoke是必需的。我读过的许多关于使用BeginInvoke的文章都没有提到使用EndInvoke。此外,我已经部署了仅使用BeginInvoke的生产代码,并没有出现任何内存问题。我使用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 是否真的做了绝对必要的任何事情,以防止您的应用程序崩溃,泄漏内存等。但这是以下事情:如果它是记录的要求,你真的应该这样做。这不仅仅是理论;这是关于在发生变化的情况下保护自己。

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.

我不是说这是一个可能的情况。我的观点只是你不应该 - 或至少我不会 - 这是真的有必要吗?使用的心态如果我可以离开它,那么我会,因为它记录了你应该这样做。

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()真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:54