2013-02-26 120 views
-1

我必須使用回調(asynchron),result..etc來調用api(SOAP)。 我有的方法使用方法:C#中的回調函數

public IAsyncResult BeginInsertIncident(
    string userName, string password, string MsgId, string ThirdPartyRef, 
    string Type, string EmployeeId, string ShortDescription, string Details, 
    string Category, string Service, string OwnerGrp, string OwnerRep, 
    string SecondLevelGrp, string SecondLevelRep, string ThirdLevelGrp, 
    string ThirdLevelRep, string Impact, string Urgency, string Priority, 
    string Source, string Status, string State, string Solution, 
    string ResolvedDate, string Cause, string Approved, AsyncCallback callback, 
    object asyncState); 

EndInsertIncident(IAsyncResult asyncResult, out string msg); 

EndInsertIncident關閉在Ticketsystem請求,並給出一個結果,如果票據被正確執行。

現狀:

server3.ILTISAPI api = new servert3.ILTISAPI(); 
api.BeginInsertIncident(username, "", msg_id, "", "", windows_user, 
    "BISS - Software Deployment", "", "", "NOT DETERMINED", "", "", "", "", "", 
    "", "5 - BAU", "3 - BAU", "", "Interface", "", "", "", "", "", "", null, 
    null); 

所以,現在,我是如何實現的回調函數時才? api「InsertIncidentCompleted」ist的狀態已經爲空,因爲我認爲不會調用EndInsertIncident。

我是C#新手,需要一些幫助。

+2

邊注:**過最差的函數簽名** – casperOne 2013-03-01 13:21:28

回答

0

AsyncCallback是一個委託,它返回void並且採用IAsyncResult類型的一個參數。

因此,創建與此簽名的方法,並把它作爲倒數第二個參數:

private void InsertIncidentCallback(IAsyncResult result) 
{ 
    // do something and then: 
    string message; 
    api.EndInsertIncident(result, out message); 
} 

傳遞這樣的:

api.BeginInsertIncident(username, "", msg_id, "", "", windows_user, 
    "BISS - Software Deployment", "", "", "NOT DETERMINED", "", "", "", "", "", 
    "", "5 - BAU", "3 - BAU", "", "Interface", "", "", "", "", "", "", 
    InsertIncidentCallback, null); 

如果你不能讓api成員變量,並希望將其傳遞給你的回調,你將不得不這樣做:

private void InsertIncidentCallback(server3.ILTISAPI api, IAsyncResult result) 
{ 
    // do something and then: 
    string message; 
    api.EndInsertIncident(result, out message); 
} 

爲了能夠通過這個作爲回調,則必須使用委託:

api.BeginInsertIncident(..., r => InsertIncidentCallback(api, r), null); 
+0

@ManuelFischer:你需要做'api'一包含您的方法的類的成員變量。 – 2013-02-26 10:29:30

+0

我可以調用方法InsertIncidentCallback與api變量,如.... InsertIncidentCallback(api),null); ? – mnlfischer 2013-02-26 10:32:29

+0

@ManuelFischer:請參閱更新。 – 2013-02-26 10:37:19