Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my web page i am calling a wcf service asynchronously which is returning a value .My problem is that my wcf service is getting called and its returning also right value .
But how to bind this value returned to a text box in my page as its a background thread diff from main thread ?
Posted

Here Is my Example
C#
   private void button1_Click(object sender, EventArgs e)
        {
            ThreadClass th = new ThreadClass();

            th.ThreadMth(8, 10, (report) => 
            {
                this.Invoke(new Action(() =>  //invoke main Thread
                {
                    listBox1.Items.Add(report);
                }));

            });
        }
    }

    public class ThreadClass
    {
        private delegate int myDelegate(int x, int y);

        Action<int> myActionReport = (v1) => { }; //Lambda

        public void ThreadMth(int xX, int yY, Action<int> actionReport)
        {
            myDelegate Sum = (x, y) =>
                {
                    Thread.Sleep(5000);
                    return x + y;
                };

                 Sum.BeginInvoke(xX, yY, result =>
                     //Lamnda pass int x, int y, AsyncCallBack 
                     //using Lambda exp., & pass Sum (delegate) as object
                     {
                        myDelegate ss = (myDelegate)result.AsyncState;
                        actionReport((int)ss.EndInvoke(result));
                     }, Sum);
       }
    }
</int></int>

So, you build delegates and methods with same signature( point delegate to method). if you want new thread delegate.BeginInvoke;
I like to use Lambda, just like in code.
Sorry for my English (I am from Latvia)
good luck!

this is how I do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900