Powershell Windows Forms更新滞后 [英] Powershell Windows Forms update lag

查看:94
本文介绍了Powershell Windows Forms更新滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的工作制作报告工具。 该工具有4个表,每隔几分钟刷新一次。 刷新时会创建后台作业以查询下次刷新的新信息。 在手动刷新期间,我想要将表格中的
灰色,并等待该后台作业完成,然后再恢复功能。一切都很好。 (下面的代码)

I am working on a reporting tool for my work.  The tool has 4 tables in it that refresh every few minutes.  When it refreshes it creates a background job to query new information for the next refresh.  During a manual refresh I want to grey out the tables, and have it wait for this background job to complete before resuming functionality. That all works great. (code below)

令我感到困惑和困惑的是灰色过程似乎滞后。 当它自己独立运行时。 然而,随后的睡眠线似乎做了1个表,然后睡了一秒..然后再多吃2个..然后睡一个
秒。 并且它与睡眠间隔保持一致。 这意味着如果我将睡眠间隔设置为3秒,我的桌子的分段灰度将错开3秒而不是1秒。

What has me completely confused and baffled is that the grey out process seems to lag.  When run by itself its instantaneous.  However with the sleep line afterwards it seems to do 1 table, then sleep a second.. then MAYBE 2 more.. then sleep a second.  And its consistent to the sleep interval.  Meaning if I set the sleep interval to 3 seconds, the staged grey'ing of my tables becomes staggered by 3 seconds instead of 1.

所以我正在寻找更好的解决方案,同时也是我不介意知道PowerShell如何在完成前一行代码之前转移到下一行代码。

So I am looking for a better solution, and also wouldn't mind knowing how PowerShell can move to the next line of code before completing a previous one.

$myWindow.add_KeyDown({
    if ($_.keycode -eq "F5") {
        for ($i=0; $i-lt 4; $i++ ) {
            foreach ($row in $myDataGrid[$i].Rows) {
                $row.DefaultCellStyle.BackColor = "GRAY" 
                $row.DefaultCellStyle.ForeColor = "BLACK" 
            }
        } 
        while ((get-job -Name "ticker").state -ne "Completed") {sleep 1}
        $out = Receive-Job -name "ticker"
        Get-Job -name "ticker" | Remove-Job
        refresh $($out) #updates the tables
        Start-Job -Name "ticker" -ScriptBlock $query -ArgumentList $args
    }
})

推荐答案

如果事件处于循环中,则无法设计自适应表单。 这是非程序员理解表单最困难的事情之一。 事件应该只执行非常少量的代码。它可以开始工作然后退出。 您
可以使用计时器轮询作业以完成。

You cannot design responsive forms if the event sits in a loop.  This is one of the hardest things for non programmers to understand about forms.  An event should execute only a very small amount of code. It can start a job then exit.  You can use a timer to poll the job for completion.

这是一篇关于如何创建响应表单的博客:
https://www.sapien.com/blog/?s=+responsive+forms

Here is a blog on how to create responsive forms: https://www.sapien.com/blog/?s=+responsive+forms

以下是有关如何构建优秀表单的更多说明:
https: //www.sapien.com/blog/topics/user-interface-design-for-administrators/

Here are more instructions on how to build good forms: https://www.sapien.com/blog/topics/user-interface-design-for-administrators/


这篇关于Powershell Windows Forms更新滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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