如何在for循环中显示连续变化的值 [英] How do I display continuous changing values inside a for loop

查看:107
本文介绍了如何在for循环中显示连续变化的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

在下面的代码中,我想在文本框中连续显示外部循环编号(带下划线)。但它不是连续显示,它只在循环结束时显示计数。





for(int i = 0; i < noOfSim; i ++)

{

textBox1.Text =(i + 1).ToString();



for(int j = 0; j< lstSimulData.Count; j ++)

{

int pRand = Possion_Rand(lstSimulData [ j] .Lambda);

double lRand = LogNorm_Rand(lstSimulData [j] .Mu,lstSimulData [j] .Sigma,pRand);

arrLossamts [j,i] = lRand;

}

}



我将非常感谢任何帮助/指针。

I am facing the following problem :
In the following code,i want to continuously display the outer loop number in a text box (underlined). But it is not displaying continuously, it displays the count only at the end of loop.


for (int i = 0; i < noOfSim; i++)
{
textBox1.Text = (i + 1).ToString();

for (int j = 0; j < lstSimulData.Count; j++)
{
int pRand = Possion_Rand(lstSimulData[j].Lambda);
double lRand = LogNorm_Rand(lstSimulData[j].Mu,lstSimulData[j].Sigma,pRand);
arrLossamts[j, i] = lRand;
}
}

I will be highly grateful for any help/pointers.

推荐答案

这是因为实际运行循环的线程和更新UI的线程是唯一的线程。



你必须在一个单独的线程上运行循环,并设置一个委托,以便能够从它更新UI线程。



最好的方法是使用一个BackgroundWor KER。这样:

That is because the thread actually running the loop and the thread updating the UI are the same only thread.

You have to run the loop on a separate thread, and setup a delegate to be able to update the UI thread from it.

Best way would be to use a BackgroundWorker. This way:
public partial class Form1 : Form
{
   public Form1() {
      InitializeComponent();
      BackgroundWorker bw = new BackgroundWorker();
      bw.DoWork += Run;
      bw.RunWorkerAsync();
   }

   void Run(object sender, DoWorkEventArgs e) {
      for (int i = 0; i < noOfSim; i++) {
         Update(i + 1);
         for (int j = 0; j < lstSimulData.Count; j++) {
            // ...
         }
      }
   }

   public delegate void IntConsumerControl(Control control, int value);

   public void SetText(Control control, int value) {
      if (control.InvokeRequired) {
         control.Invoke(new IntConsumerControl(SetText), new object[] { control, value });
      } else {
         control.Text = value.ToString();
      }
   }

   void Update(int value) {
      SetText(textBox1, value);
   }
}





还有其他方法,但这个方法有效。

希望这会有所帮助。



There are other ways, but this one works.
Hope this helps.


这是正确的,现代计算机对于这样一个平凡的循环来说太快了。您可以使用计时器来控制速度,如下所示:

That is correct, the modern computer is too fast for such a mundane loop. You can use a timer to control the pace, like this:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplicationCS
{
    public partial class Form17 : Form
    {
        Timer timer = new Timer();
        int i;

        public Form17()
        {
            InitializeComponent();
            i = 0;
            timer.Tick += new EventHandler(timerTick); // register a timer event
            timer.Interval = 500;              // Timer will tick every half a second
            timer.Enabled = true;              // Enable the timer
            timer.Start();                     // Start
        }

        private void timerTick(object sender, EventArgs e)
        {
            if (i < 10)
            {
                i++;
                textBox1.Text = i.ToString();
            }
            else
            {
                timer.Stop();
            }
        }
    }
}


这篇关于如何在for循环中显示连续变化的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆