Winform关于跨线程问题变量.请以代码见真章.
一个静态变量,是经常需要复制的. 两个文本框,一个按钮在单击事件时,当this.textBox1.Text赋值给textbox2.Text; 常量 Count_A 要锁住不能为textBox1赋值.
public partial class Form1 : Form
{
public static string Count_A = "阿啵次德俄佛格";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread th1 = new Thread(new ThreadStart(Run));
Thread th2 = new Thread(new ThreadStart(Run));
th1.Name = "线程1";
th2.Name = "线程2";
th1.Start();
}
/// <summary>
/// 线程 执行方法 Run
/// </summary>
public void Run() {
// MessageBox.Show("常量"+Count_A);
while (string.IsNullOrEmpty(Count_A))
{
Monitor.Enter(this);
MessageBox.Show(Thread.CurrentThread.Name + ": 正在启动 ");
this.textBox1.Text =Count_A;
// this.textBox2.Text = Count_A; 线程异常
MessageBox.Show("常量:" + Count_A);
Monitor.Exit(this);
MessageBox.Show(Thread.CurrentThread.Name +" : 已启动完毕");
}
}
private void button1_Click(object sender, EventArgs e)
{
} --------------------编程问答-------------------- 你这个程序可以执行吗?对控件进行了跨线程的访问。 --------------------编程问答--------------------
我想实现的是这样的: 父线程进行时有个值复制,然后子线程然后接受父线程这个值.
补充:.NET技术 , C#