异步就是开启一个新的工作线程吗

  MessageBox.Show("当前线程唯一标示:"+Thread.CurrentThread.ManagedThreadId.ToString());
            var request = HttpWebRequest.Create("http://www.baidu.com");
            request.BeginGetResponse((IAsyncResult ar) =>
            {
                MessageBox.Show("当前线程唯一标示:" + Thread.CurrentThread.ManagedThreadId.ToString());
                WebRequest req = ar.AsyncState as WebRequest;
                var response = request.EndGetResponse(ar);
                var stream = response.GetResponseStream(); 
                using (StreamReader reader = new StreamReader(stream))
                {
                    var content = reader.ReadLine(); 
                    //this.invoke是在拥有label1的窗口的线程上执行一个委托 否则在新线程里操作label1会跨线程 报错
                    this.Invoke(new MethodInvoker(() => {
                        label1.Text = content;
                    })); 
                }

            }, request);

 

posted @ 2013-03-08 13:37  Ganler1988  阅读(560)  评论(0编辑  收藏  举报