作者julietgirl76 (小涵)
看板C_Sharp
标题[问题] 不同class不同thread改变form元件文字
时间Fri Oct 15 18:16:55 2010
大家好:
最近在碰执行绪的东西,终於可以传递参数到执行绪以及在执行绪中修改Form元件属性了
但是目前又碰到一个问题,如果是想在不同class之间不同thread修改form的元件属性,我
新创了一个
class以下为程式码
namespace ImageCorp
{
public delegate void changeButtonTextCallBack(string myStr, Control ctl);
public class UIController
{
public static void changeButtonText(string myStr, Control ctl)
{
Form1 f1 = new Form1();
if (f1.InvokeRequired)
{
changeButtonTextCallBack myUpdate = new
changeButtonTextCallBack(changeButtonText);
f1.Invoke(myUpdate, myStr, ctl);
}
else
{
ctl.Text = myStr;
}
}
}
}
而Form1 的程式码为
namespace ImageCorp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread aThread = new Thread(changeButtonTextFunction);
aThread.Start();
}
private void changeButtonTextFunction()
{
UIController.changeButtonText("aaa", button1);
}
}
}
但是执行出来会出现跨执行绪无效...不知道该往哪个方向去找资料呢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.22.46.91
1F:→ julietgirl76:找到方法了..原来是自己观念上的问题^^" 10/15 19:25