第二个 BeginInvoke 调用声明已完成。为什么?

发布于 2024-09-24 16:15:11 字数 493 浏览 5 评论 0原文

我反复使用 BeginInvoke 调用一个方法。每次调用后,我都会调用 EndInvoke。

问题在于,对于第二次调用,返回的 IAsyncResult 中的 IsCompleted 成员在 BeginInvoke 调用后立即设置为 true。

这会导致故障,因为程序随后认为第二次调用已完成。

为什么要这样做,以及如何检测第二次调用何时真正完成?

Declarations:
IAsyncResult ar;
Mercury.J4000.Ui.frmMain.doPrintsDelegate printCallback;


The BeginInvoke call:
ar = printCallback.BeginInvoke (jobNameArray, copies, distances, null, null);


The EndInvoke call (in another method):
printCallback.EndInvoke(ar);

I'm repeatedly calling a method with BeginInvoke. After each call, I call EndInvoke.

The problem is that for the second call, the IsCompleted member in the returned IAsyncResult is set to true IMMEDIATELY after the BeginInvoke call.

This causes a malfunction, because the program then thinks the second call is done.

Why does it do this, and how can I detect when the 2nd call is REALLY completed?

Declarations:
IAsyncResult ar;
Mercury.J4000.Ui.frmMain.doPrintsDelegate printCallback;


The BeginInvoke call:
ar = printCallback.BeginInvoke (jobNameArray, copies, distances, null, null);


The EndInvoke call (in another method):
printCallback.EndInvoke(ar);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-10-01 16:15:11

我假设您正在做这样的事情:

IAsyncResult ar;
ar = printCallback.BeginInvoke(...);
// do some other stuff
ar = printCallback.BeginInvoke(...);

如果第一个异步操作在第二个异步操作开始之前尚未完成,那么您将丢失 IAsyncResult 值。它被第二次调用覆盖。

无论如何,您必须跟踪各个 IAsyncResult 引用并将它们与您进行的调用关联起来。如果只有一两个变量,您可以轻松跟踪单个变量或集合(通常是 List 或数组)。

这个问题可能会帮助您:我可以使用委托的单个实例来启动多个异步请求吗?

我还建议您考虑对异步委托使用回调。这样,正确的 IAsyncResult 将作为参数传递给回调,并且不会出现歧义。但是,使用 AsyncCallback 可能需要对代码进行重大重构。

I assume you're doing something like this:

IAsyncResult ar;
ar = printCallback.BeginInvoke(...);
// do some other stuff
ar = printCallback.BeginInvoke(...);

If the first async operation isn't finished before the second one starts, then you're going to lose the IAsyncResult value. It's overwritten by the second call.

Somehow, you have to keep track of the individual IAsyncResult references and associate them with the calls that you made. If you only have one or two, you can easily keep track with individual variables, or a collection (List or array, typically).

This question might help you out: Can I use a single instance of a delegate to start multiple Asynchronous Requests?

I also suggest that you consider using callbacks for your asynchronous delegates. That way, the proper IAsyncResult is passed as a parameter to the callback, and there's no chance for ambiguity. Using an AsyncCallback, however, might require major refactoring of your code.

猫弦 2024-10-01 16:15:11

我的猜测是您在两次调用中使用相同的“ar”变量,因为您已将其声明为字段。每个调用都应该有自己的实例;将该字段声明为 List 并编写必要的管道来初始化它并将每个结果与每个调用相关联。您没有为我们提供足够的代码来帮助您完成该部分(事实上,这个答案是一个猜测,因为您没有提供足够的代码。)

My guess is that you are using the same "ar" variable in both calls because you have declared it as a field. Each call should have its own instance; declare the field as List<IAsyncResult> instead and write the requisite plumbing to initialize it and correlate each result with each call. You haven't given enough code for us to help you with that part (in fact this answer is a guess because you haven't given enough code.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文