bluesky_lcj

导航

在C#中跨线程访问Winform控件

 private void requestCompleted(IAsyncResult asyncResult)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls   =   false;
            StringBuilder ss = new StringBuilder();
            if (asyncResult == null) return;
            System.Net.HttpWebRequest hwr = asyncResult.AsyncState as System.Net.HttpWebRequest;
            System.Net.HttpWebResponse response =
              (System.Net.HttpWebResponse)hwr.EndGetResponse(asyncResult);
            System.IO.StreamReader sr = new  System.IO.StreamReader(response.GetResponseStream());
            ss.Append(sr.ReadToEnd().ToString());

            this.textBox1.Text = ss.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.HttpWebRequest request =
            (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.cnblogs.com");
            IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request);    
        }


【System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls   =   false; 】可以解决线程间调用无效的问题。


posted on 2009-08-04 09:09  bluesky_lcj  阅读(249)  评论(0编辑  收藏  举报