作者glenn7012 (垃圾桶)
看板C_Sharp
标题Re: [问题] 请问如何自动更新Form
时间Thu Oct 6 23:59:46 2005
※ 引述《saker ()》之铭言:
: 请问如何让Form作自动的更新
: 假设我按下一个button
: 在button_click的event下 我写了一个回圈
: 想要将Form上的object作依次移动(利用修改location)
: 但是我这种执行结果最後只会出现最後的那个位置
: 而没有每修改一次location 就出现一次
: 我试用过override Reflesh() 但是也是不行
: 所以想请问可以用那个event 将我每次更改form上object的location
: 的结果都show出来
: ex : object的location 由(1,1) -> (2,2) -> (3,3)
: 我现在只会直接出现(3,3)的画面
: 谢谢各位的帮忙
我的认知 简单说
Form本身是一个UI thread
像回圈或是IO等需要占时间处理 不要写在UI thread里
而放到另一个background thread
在background thread有需要变动UI时 再invoke讯息到UI thread
程式修改如下
private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread backgroundthread=
new System.Threading.Thread(new System.Threading.ThreadStart(process));
backgroundthread.Start();
}
delegate void delegate_updateUI(int x,int y);
private void updateUI(int x,int y)
{
//更改form上object的location
}
private void process() //background thread
{
for (XXXX)
{
int x,y;
// processing
// 送讯息到UI thread
this.Invoke(new delegate_updateUI(this.updateUI),new object[] {x,y});
System.Threading.Thread.Sleep(100);
}
}
以上分两个thread後 for回圈就不会影响UI运作了
尤其回圈越久 UI运作相对越顺畅
--
赚P币ing
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.74.223.14