Navigation

Saturday 30 May 2015

Invoke Method Asynchronous By Delegate

aspx code


<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function fnSaveHours() {
            debugger;
            $.ajax({
                type: "POST",
                url: "default.aspx/saveHours",
                data: "{}",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: false,
                success: function (json, result) {
                    alert(result);
                },
                error: function (a, b, c) {

                    alert(a + '<br/>' + b + '<br/>' + c);
                },
                failure: function () {
                    alert("Sorry,there is a error!");
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                <input id="Button1" type="button" value="button" onclick="fnSaveHours();" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>


C# code


public delegate string DelegateWithOutAndRefParam(string arg1, out int arg2, ref ArrayList list);
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string saveHours()
        {
            _default abc = new _default();          
            for (int i = 0; i < 5; i++)
            {
                abc.CallfnWithOutAndRefParameters();                
            }
            return "success";
           
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                CallfnWithOutAndRefParameters();               
            }
        }       
        private string fnWithOutAndRefParam(string arg1, out int arg2, ref ArrayList list)
        {

            Thread.Sleep(3000);
            // lets modify the data!
            arg1 = "Modify Value for param1";
            arg2 = 200;
            list = new ArrayList();
            return "Thank you for reading this article";

        }
        private void CallfnWithOutAndRefParameters()
        {
            // create the args to pass to the function
            string strArg1 = "arg1";
            int intValue = 100;
            ArrayList list = new ArrayList();
            list.Add("Item1");

            // create the delegate
            DelegateWithOutAndRefParam delfn = new DelegateWithOutAndRefParam(fnWithOutAndRefParam);
            // call the beginInvoke function!
            IAsyncResult tag = delfn.BeginInvoke(strArg1, out intValue, ref list, null, null);
            // normally control is returned right away,
            // so you can do other work here...

            // calling end invoke notice that intValue and list are passed
            // as arguments because they might be updated within the function.
            string strResult = delfn.EndInvoke(out intValue, ref list, tag);
            // write down the args:

            Label1.Text += "arg1: " + strArg1;
            Label1.Text +=" arg2: " + intValue;
            Label1.Text += " ArrayList count: " + list.Count;
            Label1.Text += " return value: " + strResult+"<br/>";
            
        }
       
    }

No comments:

Post a Comment