跨线程操作无效:控制'richTextBox2'

时间:2013-03-23 12:19:42

标签: c# multithreading winforms exception

我编写多线程应用并获得异常Cross-thread operation not valid: Control 'richTextBox2' accessed from a thread other than the thread it was created on. 这是我的代码:

    Thread[] thr;
    static object locker = new object();
    string titles_path, routes_path, mamas_path, descriptions_path, site_urls_path;
    private void button1_Click(object sender, EventArgs e)
    {
        decimal value = numericUpDown1.Value;
        int i = 0;
        int j = (int)(value);
        thr = new Thread[j];
        for (; i < j; i++)
        {
            thr[i] = new Thread(new ThreadStart(go));
            thr[i].IsBackground = true;
            thr[i].Start();
        }
    }

    public void go()
    {
        while (true)
        {
            string mama = "";
            string ua = HttpHelper.ChromeUserAgent();
            string[] site_url = new string[20];
            lock (locker)
            {
                if (mamas.Count == 0)
                {
                    break;
                }
                else
                    mama = mamas.Dequeue();
            }

            Sed sed = new Sed(mama, ua);
            string auth = sed.Auth(textBox1.Text.Split(':')[0], textBox1.Text.Split(':')[1].Trim());
            if (auth == "Login")
            {
                lock (locker)
                {
                    for (int runs = 0; runs < 20; runs++)
                    {
                        if (site_urls.Count == 0)
                        {
                            break;
                        }
                        else
                            site_url[runs] = site_urls.Dequeue().Replace("http://", "");
                    }
                }
                string add_domains = sed.add_domains(site_url);
                if (add_domains == "Added")
                {
                    for (int runs = 0; runs < 20; runs++)
                    {
                        string dorway = site_url[runs];
                        if (string.IsNullOrEmpty(dorway))
                            break;
                        string title = rand_title();
                        string description = rand_description();
                        string route = rand_route();
                        string[] rtfLines = new string[richTextBox2.Lines.Length];
                        var num = xNet.Rand.Next(0, richTextBox2.Lines.Length);
                        string template = rtfLines[num];

                        string set_config = sed.set_config(dorway, title, description, route, template);
                        if (set_config == "Saved")

请帮助,使之工作/

0 个答案:

没有答案