如何从后台线程更新主线程? [英] how to update the main thread from the background thread ?

查看:131
本文介绍了如何从后台线程更新主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页中,我正在异步调用wcf服务,该服务正在返回一个值.我的问题是我的wcf服务正在被调用,并且其返回的值也正确.
但是如何绑定此值返回到我页面中的文本框,作为它与主线程的后台线程差异?

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 ?

推荐答案

您是否已对此进行了检查,

http://stackoverflow.com/Questions/122882/net-how-to-have-background-thread-signal-main-thread-data-is-available [ http://msdn.microsoft.com/en-us/library/system.componentmodel. backgroundworker.aspx [ ^ ]
have u checked this,

http://stackoverflow.com/questions/122882/net-how-to-have-background-thread-signal-main-thread-data-is-available[^]


http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]


这是我的示例
Here Is my Example
   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>


因此,您可以使用相同的签名构建委托和方法(将委托指向方法).如果要新的线程委托.BeginInvoke;
我喜欢使用Lambda,就像在代码中一样.
对不起,我的英语(我来自拉脱维亚)
祝你好运!

这就是我的方法.


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.


这篇关于如何从后台线程更新主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆