[問題] 不同class不同thread改變form元件文字
大家好:
最近在碰執行緒的東西,終於可以傳遞參數到執行緒以及在執行緒中修改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
→
10/15 19:25, , 1F
10/15 19:25, 1F