Windows窗体应用程序循环不起作用

时间:2016-02-05 17:42:40

标签: c# windows forms for-loop

Compare-Object -ReferenceObject $MaintenanceWindows -DifferenceObject $ScomAlerts -Property Computername -IncludeEqual -PassThru |
Where-Object { $_.SideIndicator -match '==|=>' } |
Select-Object ComputerName, @{n="Collection Name";e={ if($_."Collection Name"){ $_."Collection Name" } else { "Not Found!" } }}

Computername Collection Name                                 
------------ ---------------                                 
Server2      NA - All DA Servers - Patching - Cert - Thu 2:00
Server3      Not Found!

这是我在Windows窗体应用程序中使用的代码部分 我希望它输入值并显示它上升但是它只是跳到最后并放置最后一个输出而不是计数。 有些人说使用计时器,但我无法让他们上班。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在使用循环锁定UI线程,以便在完成其工作之前不会更新控件。当循环完成并且UI刷新时,您最终只能看到最终值。

请改为使用Timer控件。您可以告诉它定期举起一个事件,它将允许您的UI正确更新。

Timer添加到Form,然后将以下代码插入构造函数中以进行尝试。目前,它每1毫秒更新一次你的元素(实际上,它不会那么快)。

int i = 0;
int j = 10;

timer1.Interval = 1;
timer1.Tick += (s, e) =>
{
    string y = i.ToString();
    webBrowser1.Document.GetElementById("lst-ib").SetAttribute("value", y);

    i++;
    j++;

    if (j > 1000)
        timer1.Stop();
};

timer1.Start();