标题:关于 线程和windows窗口的问题
只看楼主
zhoudashao
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2005-8-4
 问题点数:0 回复次数:3 
关于 线程和windows窗口的问题

在线程里对windows的窗口修改 是怎么实现的啊?是不是不能在同有一 个类里面调用呢? 比如 我要在一个线程的方法里 对textbox.text附值 要怎么做啊?象下面的程序 运行到 richTextBox1.Text = str1 时,编译器就说:"线程间操作无效:,从不是创建控件“richTextBox1”的线程访问它" using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading;

namespace WindowsApplication1 { public partial class Form1 : Form { public static string str1; public static string str2; bool b1 = false; bool b2 = false; public Form1() { InitializeComponent(); }

private void Form1_dgCount1() { richTextBox1.Text = str1; }

private void Form1_dgCount2() { richTextBox2.Text = str2; } public void WorkerThread1() { Thread.CurrentThread.Name = "11"; //MessageBox.Show(Thread.CurrentThread.Name); //Form1 myform = new Form1(); for (int i = 1; i <= 100; i += 2) { str1 = str1 + i.ToString() + " "; // myform.Form1_dgCount1(); this.Form1_dgCount1(); System.Threading.Thread.Sleep(10);

} this.b1 = true; if (b2) { this.Form1_BackColorChanged(this, null); } Thread.CurrentThread.Abort();

}

public void WorkerThread2() { //Form1 myform2 = new Form1(); Thread.CurrentThread.Name = "12"; //MessageBox.Show(Thread.CurrentThread.Name); for (int j = 2; j <= 100; j += 2) { str2 = str2 + j.ToString() + " "; // myform2.Form1_dgCount2(); this.Form1_dgCount2(); System.Threading.Thread.Sleep(10);

} this.b2 = true; if (b1) { this.Form1_BackColorChanged(this, null); } Thread.CurrentThread.Abort();

} private void Form1_BackColorChanged(object sender, System.EventArgs e) { this.BackColor = Color.Red; }

private void button1_Click(object sender, EventArgs e) { ThreadStart worker1 = new ThreadStart(WorkerThread1); ThreadStart worker2 = new ThreadStart(WorkerThread2);

Thread t1 = new Thread(worker1); Thread t2 = new Thread(worker2);

t1.Start(); t2.Start(); } } }

搜索更多相关主题的帖子: 线程 windows using System 窗口 
2005-09-06 09:56
zhoudashao
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2005-8-4
得分:0 
期待啊..........
2005-09-08 09:23
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
得分:0 
这个还是与.NET或者说IL的某些特性有关,换个方式吧,不用事件,普通点的方法

寻求挑战,追求完美 Oh,my god!
2005-09-09 15:34
zhoudashao
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2005-8-4
得分:0 
呵呵 我知道了可以用inVote可以实现,但是难就在:怎么传递参数到线程的方法中去?如上面的程序要向pForm1_dgCount1()方法中传递参数,是件困难的事啊!~~有哪位高手支招啊!~~!!!!!!
2005-09-12 13:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-26823-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.332453 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved