2013-02-28 71 views
0

我有一些我的方法的問題,我認爲,一個WaitHandle是解決方案。C#等待句柄回調

我通過API創建了一個事件到Ticketsystem。

private void create_Incident(string computer_idn, string swidn_choice, 
string swName_choice, string CI_Number, String windows_user) 
{ 
    string msg_id = "" + computer_idn + "_" + swidn_choice + ""; 
    string username = "BISS"; 

    hajas211ilt3.ILTISAPI api = new hajas211ilt3.ILTISAPI(); 
    api.BeginInsertIncident(username, "", msg_id, "", "", "2857", 
    "BISS - Software Deployment", "", "", "NOT DETERMINED", 
    "", "", "", "", "", "", "5 - BAU", "3 - BAU", "", 
    "Interface", "", "", "", "", "", "", 
    delegate(IAsyncResult r) 
    { InsertIncidentCallback(api, r, username, msg_id); }, null); 

} 

該方法在create_Incident方法中稱爲回調。

private void InsertIncidentCallback(server3.ILTISAPI api, IAsyncResult result, 
string username, string msg_id) 
{ 
    api.EndInsertIncident(result, out message); 
} 

我需要在一個if的消息,但我需要安全,有一條消息。所以我必須等待InsertIncidentCallback和消息。

在其他方法我問,如果消息是確定的:

private void checkIncident(string omputer_idn, string swidn_choice, 
          string swName_choice, string CI_Number, 
string windows_user) 
    { 
     if (message == null) 
     { 
      string responseXML; 
      api.REMEDY_ReadResponseXML(username, out responseXML, out msg_id); 
      XDocument doc = XDocument.Parse(responseXML); 
      inquiryId = (string)doc.Root.Element("inquiry_id"); 

      if (inquiryId == null || inquiryId == "") 
      { 
       information_text.Text = "....."; 
      } 
      else 
      { 
       information_remedyID.Visible = true; 
       information_remedyID.Text = inquiryId; 
       create_LanDesk(computer_idn, swidn_choice, 
       swName_choice, inquiryId); 
      } 
     } 
     else 
     { 
      information_text.Visible = true; 
      information_text.Text = "...."; 
     } 
    } 

我如何能實現一個WaitHandle的回調? WaitHandle在這種情況下是正確的選擇嗎?

+0

你沒有真正說出問題所在,只是說你的某些方法有問題。 – webnoob 2013-02-28 12:44:29

+0

我怎樣才能實現一個WaitHandle的回調? WaitHandle在這種情況下是正確的選擇嗎? – mnlfischer 2013-02-28 12:46:48

+0

在什麼情況下?你說有一個問題讓你想到等待處理,但是那個問題是什麼?即''我的一些方法有問題,我認爲,WaitHandle是解決方案。' – webnoob 2013-02-28 12:47:29

回答

1

在您的AppPool線程上使用WaitHandle,從而導致它們無限期地等待會很快使您的服務器癱瘓。

不要這樣做。

有幾種方法可以在網上使用來解決這個問題。

當您發送此回覆時,您可能會向用戶顯示一條消息,指出「正在處理」。

當您收到響應存儲該異步調用的結果時,可以使用類似SignalR的框架將其推送給用戶。

或者,您可以將結果存儲在Session變量中,並使用UpdatePanel在間隔後刷新並顯示狀態。

+0

服務器爲何應該停機? – mnlfischer 2013-02-28 12:48:12

+0

Asp.net在AppPool中的線程數量有限(25到100之間取決於您的服務器)。每個請求都需要一個線程。如果您鎖定這些線程,則服務器無法處理請求。如果您必須讓頁面等待,請搜索如何使用「異步頁面」。這些將允許你使用'InsertIncidentCallback'中的'IAsyncResult.AsyncWaitHandle'並且不阻塞線程 – nunespascal 2013-02-28 13:14:00