C# Wait Handle for Callback

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 14:09:47

问题


I have a Problem with some of my Methods and I think, a WaitHandle is the solution.

I create a Incident through a API to a 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);

}

This method called as a callback in the create_Incident method.

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

I need the message in a if, but I need the safety, that there is a message. So I have to wait for the InsertIncidentCallback and the message.

In a other Method I ask, if the message is ok:

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 = "....";
        }
    }

How can I implement a WaitHandle to the Callback? Is the WaitHandle the right option in this case?


回答1:


Using a WaitHandle on you AppPool threads, thus causing them to wait indefinitely would quickly bring your server to its knees.

Don't do it.

There are several alternatives that you can use on the web to work around this.

You could display a message to the user saying "Processing" when you send this reply.

When you receive the response store the result on that async call you can push it to the user using a framework like SignalR.

Alternatively you could store the result in a Session variable and use an UpdatePanel to refresh and show the status after an interval.



来源:https://stackoverflow.com/questions/15135653/c-sharp-wait-handle-for-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!