Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 有一个iSyncResult火灾“a”;“已完成”;事件_.net_Delegates_Remoting - Fatal编程技术网

.net 有一个iSyncResult火灾“a”;“已完成”;事件

.net 有一个iSyncResult火灾“a”;“已完成”;事件,.net,delegates,remoting,.net,Delegates,Remoting,我正在使用远程处理执行一些IPC通信,有以下问题: 当我调用委托来收集服务器端数据时,在收集IAsyncResult时是否可能在客户端触发事件 下面的代码是用VC++编写的,但是.net代码是.net代码 GetFileTextDelegate ^svd = gcnew GetFileTextDelegate(obj, &BaseRemoteObject::GetFileText); IAsyncResult ^arValSet = svd->BeginInv

我正在使用远程处理执行一些IPC通信,有以下问题:

当我调用委托来收集服务器端数据时,在收集IAsyncResult时是否可能在客户端触发事件

下面的代码是用VC++编写的,但是.net代码是.net代码

  GetFileTextDelegate ^svd = gcnew GetFileTextDelegate(obj, &BaseRemoteObject::GetFileText);
  IAsyncResult        ^arValSet = svd->BeginInvoke(nullptr, nullptr);
  String ^result = svd->EndInvoke(arValSet);
在本例中,最后一行代码将锁定当前线程,直到调用完成。是否有可能只是向“IAsyncResult_Completed”事件或类似事件进行贿赂

谢谢

是的,MSDN上有一个关于使用委托执行此操作的说明

最简单的方法是对委托使用
BeginInvoke
,并将
AsynCallback
委托的实例传递给它,以便在操作完成时调用

在C#(我比托管C++更熟悉)中,您基本上可以编写:

// when the async operation completes, YourCallBackMethod will be run
AsyncCallback callback = new AsynCallback( YourCallBackMethod );
callAsynchronously.BeginInvoke( callback, null );

在回调方法中,必须对异步运行的委托调用
EndInvoke()
。最简单的方法是使用提供的
IAsyncResult

您应该能够将委托(异步回调)传递给BeginInvoke,它将在异步方法完成执行后执行

下面是一个简单但直截了当的示例:

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

class MainClass
{
  delegate int MyDelegate(string s);

  static void Main(string[] args)
  {
    MyDelegate X = new MyDelegate(DoSomething);
    AsyncCallback cb = new AsyncCallback(DoSomething2);
    IAsyncResult ar = X.BeginInvoke("Hello", cb, null);

    Console.WriteLine("Press any key to continue");
    Console.ReadLine();
  }
  static int DoSomething(string s)
  {
    Console.WriteLine("doooooooooooooooo");
    return 0;
  }

  static void DoSomething2(IAsyncResult ar)
  {
    MyDelegate X = (MyDelegate)((AsyncResult)ar).AsyncDelegate;
    X.EndInvoke(ar);
  }
}

请求,你就会得到。搜索时,我从未想到“回调”这个词。感谢您,MSDN示例“远程处理示例:异步远程处理”提到远程处理不适用于静态函数。