C# 窗体控件问题,text属性更改后不显示。
代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;usingSyste...
代码如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace 生产者_消费者
{
public partial class Form1 : Form
{
private object thisLock = new object();
private Queue<string> list = new Queue<String>();
Thread threadPrint;
Thread threadDownload ;
private int max = 5;//仓库最大为5,可储存5个商品。
private int count = 0;
private static Form1 form = new Form1();
public Form1()
{
InitializeComponent();
}
private void startProduct_Click(object sender, EventArgs e)
{
if (this.startProduct.Text == "开始生产")
{
threadPrint = new Thread(new ThreadStart(form.product));
threadPrint.Start();
this.startProduct.Text = "停止生产";
}else if (this.startProduct.Text == "停止生产")
{
threadPrint.Suspend();
this.startProduct.Text = "继续生产";
}else if (this.startProduct.Text == "继续生产")
{
threadPrint.Resume();
this.startProduct.Text = "停止生产";
}
}
private void startConsume_Click(object sender, EventArgs e)
{
if (this.startConsume.Text == "开始消费")
{
threadDownload = new Thread(new ThreadStart(form.consume));
threadDownload.Start();
this.startConsume.Text = "停止消费";
}else if (this.startConsume.Text == "停止消费")
{
threadDownload.Suspend();
this.startConsume.Text = "继续消费";
}else if (this.startConsume.Text == "继续消费")
{
threadDownload.Resume();
this.startConsume.Text = "停止消费";
}
}
public void product()//生产者。
{
while (true)
{
if (count < max)
{
lock (this)
{
list.Enqueue("第" + (count + 1) + "个商品。");
count ++;
MessageBox.Show("生产者:生产第" + count + "个");
// this.productShow.Text = "生产者:生产第" + count + "个";
//此处控件text属性更改后不显示。 为什么?
}
}
Thread.Sleep(2000);
}
}
后面还有代码,但百度不让贴上来了。 展开
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace 生产者_消费者
{
public partial class Form1 : Form
{
private object thisLock = new object();
private Queue<string> list = new Queue<String>();
Thread threadPrint;
Thread threadDownload ;
private int max = 5;//仓库最大为5,可储存5个商品。
private int count = 0;
private static Form1 form = new Form1();
public Form1()
{
InitializeComponent();
}
private void startProduct_Click(object sender, EventArgs e)
{
if (this.startProduct.Text == "开始生产")
{
threadPrint = new Thread(new ThreadStart(form.product));
threadPrint.Start();
this.startProduct.Text = "停止生产";
}else if (this.startProduct.Text == "停止生产")
{
threadPrint.Suspend();
this.startProduct.Text = "继续生产";
}else if (this.startProduct.Text == "继续生产")
{
threadPrint.Resume();
this.startProduct.Text = "停止生产";
}
}
private void startConsume_Click(object sender, EventArgs e)
{
if (this.startConsume.Text == "开始消费")
{
threadDownload = new Thread(new ThreadStart(form.consume));
threadDownload.Start();
this.startConsume.Text = "停止消费";
}else if (this.startConsume.Text == "停止消费")
{
threadDownload.Suspend();
this.startConsume.Text = "继续消费";
}else if (this.startConsume.Text == "继续消费")
{
threadDownload.Resume();
this.startConsume.Text = "停止消费";
}
}
public void product()//生产者。
{
while (true)
{
if (count < max)
{
lock (this)
{
list.Enqueue("第" + (count + 1) + "个商品。");
count ++;
MessageBox.Show("生产者:生产第" + count + "个");
// this.productShow.Text = "生产者:生产第" + count + "个";
//此处控件text属性更改后不显示。 为什么?
}
}
Thread.Sleep(2000);
}
}
后面还有代码,但百度不让贴上来了。 展开
1个回答
展开全部
不要在线程里访问UI线程创建的控件, 用Invoke
public void product()//生产者。
{
while (true)
{
if (count < max)
{
lock (this)
{
list.Enqueue("第" + (count + 1) + "个商品。");
count++;
//MessageBox.Show("生产者:生产第" + count + "个");
// this.productShow.Text = "生产者:生产第" + count + "个";
Showproduct(count);
}
}
Thread.Sleep(2000);
}
}
private delegate void ShowproductDelegate(int count);
private void Showproduct(int count)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowproductDelegate(Showproduct), new object[] { count });
}
else
{
this.productShow.Text = "生产者:生产第" + count + "个";
}
}
public void product()//生产者。
{
while (true)
{
if (count < max)
{
lock (this)
{
list.Enqueue("第" + (count + 1) + "个商品。");
count++;
//MessageBox.Show("生产者:生产第" + count + "个");
// this.productShow.Text = "生产者:生产第" + count + "个";
Showproduct(count);
}
}
Thread.Sleep(2000);
}
}
private delegate void ShowproductDelegate(int count);
private void Showproduct(int count)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowproductDelegate(Showproduct), new object[] { count });
}
else
{
this.productShow.Text = "生产者:生产第" + count + "个";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询