C# 异步调用没有 EndInvoke?
以下面的类为例。
public class A
{
// ...
void Foo(S myStruct){...}
}
public class B
{
public A test;
// ...
void Bar()
{
S myStruct = new S();
test.Foo(myStruct);
}
}
现在,我希望方法调用 test.Foo(myStruct) 成为异步调用(“即发即忘”)。 bar-method 需要尽快返回。有关委托、BeginInvoke、EndInvoke、ThreadPool 等的文档并不能帮助我找到解决方案。
这是一个有效的解决方案吗?
// Is using the `EndInvoke` method as the callback delegate valid?
foo.BeginInvoke(myStruct, foo.EndInvoke, null);
Take the following classes as an example.
public class A
{
// ...
void Foo(S myStruct){...}
}
public class B
{
public A test;
// ...
void Bar()
{
S myStruct = new S();
test.Foo(myStruct);
}
}
Now, I want the method-call test.Foo(myStruct) to be an asynchronous call ('fire-and-forget'). The bar-method needs to return as soon as possible. Documentation around delegates, BeginInvoke, EndInvoke, the ThreadPool etc. isn't helping me find a solution.
Is this a valid solution?
// Is using the `EndInvoke` method as the callback delegate valid?
foo.BeginInvoke(myStruct, foo.EndInvoke, null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用@什么是AsyncCallback?解释的回调模型
,这样您的EndInvoke就不会位于 bar() 中,但位于单独的回调方法中。
例子中的EndRead(对应EndInvoke是在回调方法中调用CompleteRead而不是bar对应的调用方法TestCallbackAPM)
You can use the Callback model explained @ What is AsyncCallback?
That way your EndInvoke will not be in bar(), but in a separate callback method.
In the example, the EndRead (corresponding to EndInvoke is in the callback method called CompleteRead rather than the calling method TestCallbackAPM corresponding to bar)
这是一个选项:
This is an option:
您不需要调用
EndInvoke
;不调用它仅仅意味着:听起来您想“一劳永逸”,因此最简单的方法是使用匿名委托,例如:
执行此代码时会发生以下情况:
del
和匿名委托 (iar => ...
)。del
。EndInvoke
时,要么返回该方法的结果,要么引发异常(如果发生)。请注意,上面的示例与:
编辑:您应该始终调用
End*
。我从未发现不调用它会出现问题的场景,但这是一个实现细节,并且是 依赖于未记录的行为。最后,如果抛出异常,您的解决方案将使进程崩溃,
如果您不关心异常,您可以简单地将 null 作为委托传递(因此,作为最后一个示例,您正在寻找的可能是:del.BeginInvoke(myStruct, null, null);
).You are not required to call
EndInvoke
; not calling it merely means:It sounds like you want to 'fire-and-forget', so the easiest way to do this is to use an anonymous delegate, for example:
This is what happens when you execute this code:
del
and the anonymous delegate (iar => ...
).del
.EndInvoke
is called the result from the method is either returned, or the exception is thrown (if one occurred).Note that the above example is very different from:
Edit: You should always call
End*
. I've never found a scenario where not calling it presents a problem, however that is an implementation detail and is relying on undocumented behavior.Finally your solution would crash the process if an exception is thrown,
you can simply pass null as the delegate if you don't care about the exception (So as a final example what you are looking for is probably:del.BeginInvoke(myStruct, null, null);
).我想说,最好的选择是使用 ThreadPool:
这会将片段排队以在单独的线程中执行。现在您还必须小心其他事情:如果您有多个线程访问
A
的同一实例,并且该实例修改了变量,那么您必须确保对该变量进行正确的同步。I would say that your best option is to use the
ThreadPool
:This will queue the snippet for execution in a separate thread. Now you also have to be careful about something else: if you have multiple threads accessing the same instance of
A
and that instance modifies a variable, then you must ensure that you do proper synchronization of the variable.