Thursday, 24 April 2014

Call method asynchronously using Delegate

        public delegate string tempDel(string str);
        protected void Page_Load(object sender, EventArgs e)
        {
            tempDel tdel = new tempDel(TestMethod);
            tdel.BeginInvoke("Ajay Patel", callback, tdel);
        }

        private void callback(IAsyncResult ar)
        {
            AsyncResult asynch = (AsyncResult)ar;
            tempDel tdel = asynch.AsyncDelegate as tempDel;
            string str = tdel.EndInvoke(ar); // this will return object,
        // you can then cast it to your return type
       }

        string TestMethod(string str)
        {
            return str;

        }

No comments:

Post a comment