我想做一個小項目來做一些計算並在列表框中添加計算結果。Rx訂閱問題,如何在Winform中更新UI控件中的數據
我的代碼:
int SumLoop(int lowLimit, int highLimit)
{
int idx;
int totalSum = 0;
for (idx = lowLimit; idx <= highLimit; idx = idx + 1)
{
totalSum += idx;
}
return totalSum;
}
private void button1_Click(object sender, EventArgs e)
{
var test2 = Observable.Interval(TimeSpan.FromMilliseconds(1000)).Select(x=>(int)x).Take(10);
test2.Subscribe(n =>
{
this.BeginInvoke(new Action(() =>
{
listBox1.Items.Add("input:" + n);
listBox1.Items.Add("result:" + SumLoop(n,99900000));
}));
});
}
其結果是:
input:0
result:376307504
(stop a while)
input:1
result:376307504
(stop a while)
input:2
result:376307503
(stop a while)
input:3
result:376307501
(stop a while)
....
...
..
.
input:"9
result:376307468
如果我想從1000修改的時間間隔恆定 - > 10,
var test2 = Observable.Interval(TimeSpan.FromMilliseconds(10)).Select(x=>(int)x).Take(10);
顯示行爲變得不同。列表框將顯示所有輸入和結果。它似乎等待所有結果完成,然後顯示一切到列表框。爲什麼?
如果我想繼續使用這個常量(間隔:10),並不想顯示一切只是一個鏡頭。我想顯示「輸入:0」 - >等待計算 - >顯示「結果:376307504」.... 那麼,我該如何做到這一點?
Thankx對您有所幫助。