AsyncCallback 可以使用非静态委托吗?

发布于 2024-09-12 22:21:52 字数 696 浏览 1 评论 0原文

我正在使用 .net 远程处理,通过异步函数调用来处理当前项目的 ipc。

我遇到了一个问题,我希望客户端:

  1. 异步请求信息
  2. 继续加载 GUI
  3. 当异步调用完成时,将其加载到 GUI

我使用以下代码执行此操作

  GetFileTextDelegate ^svd       = gcnew GetFileTextDelegate(obj, &BaseRemoteObject::GetFileText);
  AsyncCallback       ^callback  = gcnew AsyncCallback(RecievedSomething);
  IAsyncResult        ^arValSet  = svd->BeginInvoke(callback, nullptr);

在上面的示例中, 在此示例中,RecievedSomething 必须是静态方法。这是为什么?如果我可以使这个函数成为非静态的,我就可以加载我的 GUI,一切都很好。

我的解决方案是让 RecievedSomething 触发我的 gui 订阅的静态事件。这很麻烦,因为现在需要 2 个委托和一个事件来处理我的 1 个异步函数调用。

有没有办法让 AsyncCallback 与非静态函数一起工作?

谢谢!

I'm using .net remoting, with asynchronous function calls to handle the ipc of my current project.

I'm running into an issue where I'd like the client to:

  1. ASynchronously request information
  2. Continue loading the GUI
  3. When the ASynchronous call is complete, load it into the gui

I do this with the following code

  GetFileTextDelegate ^svd       = gcnew GetFileTextDelegate(obj, &BaseRemoteObject::GetFileText);
  AsyncCallback       ^callback  = gcnew AsyncCallback(RecievedSomething);
  IAsyncResult        ^arValSet  = svd->BeginInvoke(callback, nullptr);

In the above example, RecievedSomething MUST be a static method in this example. Why is that? If I could make this function non-static, I could load my gui, and alls well.

My solution is to have RecievedSomething fire off a static event that my gui subscribes to. This is cumbersome in that it will now require 2 delegates and an event to have my 1 asynchronous function call handled.

Is there a way to hace AsyncCallback work with a non-static function?

Thanks!

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

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

发布评论

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

评论(1

嗼ふ静 2024-09-19 22:21:52

您可以为 AsyncCallback 委托使用非静态方法。您只需正确指定它即可。例如,要使用 this->ReceivedSomething

AsyncCallback ^callback  = gcnew AsyncCallback(this, &MyClassType::RecievedSomething);

You can use a non-static method for your AsyncCallback delegate. You just have to specify it correctly. For example, to use this->ReceivedSomething:

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