public delegate void weituo();        private void GetCode()
        {
            WebClient wc = new WebClient();
            StreamReader strm = new StreamReader(wc.OpenRead("http://www.hao123.com"), Encoding.GetEncoding("GB2312"));
            TextBox1.Text = strm.ReadToEnd();
        }
        private void wt_GetCode()
        {
            weituo temp = new weituo(GetCode);
            Invoke(temp);
        }
        private void th_GetCode()
        {
            Thread th = new Thread(wt_GetCode);
            th.Start();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            th_GetCode();
        }
像这样使用委托的话 倒是不会抛出错误 但是在读取网页代码的时候 程序是没办法操作的 而且是白屏 显示不完整
这样的话 多线程和单线程感觉没多大区别 有没有可用的办法让他真正的多线程执行啊 

解决方案 »

  1.   

    public delegate void weituo(string s);        private void GetCode()
            {
                WebClient wc = new WebClient();
                StreamReader strm = new StreamReader(wc.OpenRead("http://www.hao123.com"), Encoding.GetEncoding("GB2312"));
                //Thread.Sleep(10000);
                weituo temp = new weituo(setvalue);
                textBox1.Invoke(temp, new object[] { strm.ReadToEnd() });
            }
            private void setvalue(string s)
            {
                this.textBox1.Text = s;
            }
            private void wt_GetCode()
            {
                GetCode();
                //weituo temp = new weituo(GetCode);
                //Invoke(temp);
            }
            private void th_GetCode()
            {
                Thread th = new Thread(wt_GetCode);
                th.Start();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                th_GetCode();
            }
      

  2.   

    在Load事件里加上这句也可以
    Control.CheckForIllegalCrossThreadCalls = false;