Re: [問題] 請問如何自動更新Form
※ 引述《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
討論串 (同標題文章)
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章