当前位置:编程学习 > C#/ASP.NET >>

怎么把多线程中的参数传出啊?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static string str_url = string.Empty;
        public delegate void SetTextHandler(string text); 
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripTextBox1.Text.Length > 0)
            {
                str_url = toolStripTextBox1.Text.Trim();
                ParameterizedThreadStart ParStart = new ParameterizedThreadStart(GetPageSource);   
                Thread myThread = new Thread(ParStart);
                //是否背景线程根据自己需要加,加上后理论上不影响界面的操作,就是不卡
                myThread.IsBackground = true;
                object o = str_url;   
                myThread.Start(o);
                //GetPageSource(toolStripTextBox1.Text.ToString().Trim());
            }
        }

        private void GetPageSource(object url)
        {
            Uri uri = new Uri(url.ToString());
            HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri);
            HttpWebResponse hwRes = (HttpWebResponse)hwReq.GetResponse();

            hwReq.Method = "Get";

            hwReq.KeepAlive = false;

            //从输入的网站提取HTML源码
            StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
            settext(reader.ReadToEnd());
        }
        private void settext(string text)
        {
            if (richTextBox1.InvokeRequired == true)
            {
                SetTextHandler set = new SetTextHandler(settext);
                richTextBox1.Invoke(set, new object[] { text });
            }
            else
            {
                richTextBox1.Text = text;
            } 

        }
    
}
}


在上面代码上
private void settext(string text)里的richtextbox1.text在其它函数里调用会出错,我想要把text这个内容在其它函数内调用,应该怎么办啊? --------------------编程问答-------------------- 你写个委托 处理到 richTextBox1.Text = text;这的时候 触发这个委托 然后把值传给这个委托 让这个委托去赋值 --------------------编程问答-------------------- 新手啊,不懂怎么委托,能写个实例么,谢谢了 --------------------编程问答-------------------- 不懂委托,那就加一行代码
    public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }



--------------------编程问答--------------------
LS的  Control.CheckForIllegalCrossThreadCalls = false; 是跨线程取值
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,