2013-10-25 28 views
1

我正在写一个FTP客户端库。我的get函数应该采用以下参数:(Server IP, remotefilename, clientfilename, Applications_callback_function)作为库中的参数的回调函数API

我已经编写了该库来执行get功能,但没有参数Applications_callback_function

期望的是写入get API和Applications_callback_function参数,并在文件传输完成后调用带有结果的回调函数,并将结果放入EventArgs

如何在C#中执行此操作?

+1

您使用委托回调参数可以做到这一点。假设库是用C#编写的... –

回答

2

您可以将代理人传递给您的get方法。试试这个:

function Get(string ServerIP, string, remotefilename, string clientfilename, Action<YourResponseType> Applications_callback_function) 
{ 
    // your logic... 
    Applications_callback_function(data_from_request); 
} 

或者,如果反应是单一类型的不是:

function Get<T>(string ServerIP, string remotefilename, string clientfilename, Action<T> Applications_callback_function) 
{ 
    // your logic... 
    Applications_callback_function(data_from_request); 
} 

// caller... 
Get<ResponseType>("127.0.0.1", "foo.txt", "bar.txt", myHandler); 

function myHandler(ResponseType data) 
{ 
    // handle response... 
} 
+0

谢谢!我喜欢在应用程序端有异步行为。我认为上面的代码同步工作,这将使应用程序等待Get函数的响应。 – Ayan

+0

那么,如果你打算将请求中的数据传递给回调函数呢? –

+0

Hi Rory McCrossan,我喜欢将Get函数的结果传递给回调函数以更新应用程序(即SUCCESS/FAILURE)。我喜欢有异议的行为,以便应用程序可以继续进行其他请求/操作。 – Ayan