这个异步 Lambda Cryptica 代码是否符合我的想法?

发布于 2024-07-22 02:49:43 字数 387 浏览 2 评论 0原文

    Action<SPItemEventProperties> deleteAction = DeleteWorkspace;
    AsyncCallback deleteDone = deleteAction.EndInvoke;
    SPSecurity.RunWithElevatedPrivileges(() => deleteAction.BeginInvoke(properties, deleteDone, null));

所以这应该异步调用DeleteWorkspace,然后在完成后调用EndInvoke,我写了它,但我不确定它会正常工作。 我逐步执行,它似乎可以工作,但语法让我自己重新猜测,因为我从未在网上看到过这样的操作...

评论?

    Action<SPItemEventProperties> deleteAction = DeleteWorkspace;
    AsyncCallback deleteDone = deleteAction.EndInvoke;
    SPSecurity.RunWithElevatedPrivileges(() => deleteAction.BeginInvoke(properties, deleteDone, null));

So this is suppose to call DeleteWorkspace Asynchronously and then call EndInvoke when its done, I wrote it but I am not positive it will work properly. I stepped through and it appears to work but the syntax is making me second guess myself cause I have never seen it done like this on the net...

Comments?

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

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

发布评论

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

评论(1

寂寞美少年 2024-07-29 02:49:43

它应该可以工作,但要真正理解它,让我们假设它是这样写的:

void RunAsync<T>(Action<T> action)
{
    AsyncCallback Done = action.EndInvoke;
    SPSecurity.RunWithElevatedPrivileges(() => action.BeginInvoke(properties, Done, null));
}

RunAsync(DeleteWorkspace);

请注意,在上面的代码中,看起来“完成”回调将立即超出范围。 但是,编译器将使用闭包捕获(关闭)它,以便在需要时可用。

It should work, but to really understand it let's pretend it were written like this:

void RunAsync<T>(Action<T> action)
{
    AsyncCallback Done = action.EndInvoke;
    SPSecurity.RunWithElevatedPrivileges(() => action.BeginInvoke(properties, Done, null));
}

RunAsync(DeleteWorkspace);

Note that in the code above, it looks like the 'Done' callback will go out of scope right away. However, the compiler will capture (close over) it with a closure, so that it's available when needed.

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