Powershell:冻结GUI [英] Powershell: Freezing GUI

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

问题描述

只是一个快速的。我遇到了一个我创建的简单工具,它可以通过一个看起来可以显示正在使用的CPU百分比的小方块获取CPU使用量一段时间(我已经为下面的代码剥离了GUI )。

$ p $ 函数循环
{
$ get = read-host
for($ start = 0; $ start -le 100; $ start ++)
{
cls
$ pro_percentage = Get-WmiObject win32_processor -computer $ get -property Loadpercentage |选择loadpercentage
$ percentage =处理器使用率为:+ $ pro_percentage.loadpercentage +%
$百分比
}
}
循环

我还创建了一些GUI,但我注意到一个majour问题,这适用于我创建的其他GUI应用程序。每当按下'go'按钮以从对话框中启动脚本时,GUI将冻结。我在GUI中创建的菜单栏冻结,没有其他按钮可以按下。



我应该在不同的线程中运行每个函数吗?类似的东西?



谢谢 解决方案

尝试添加:

  [System.Windows.Forms.Application] :: DoEvents()

在你的循环中。 快速和肮脏,但是


Just a quick one. I have a problem with a simple tool that I've created that gets the CPU usage for a set amount of time with a small box that appears to display the % of CPU being used (I've stripped out the GUI for the code below).

function loop
{
$get = read-host
for($start = 0; $start -le 100;$start++)
{
cls
$pro_percentage = Get-WmiObject win32_processor -computer $get -property Loadpercentage | select loadpercentage
$percentage = "Processor usage is: " + $pro_percentage.loadpercentage + "%"
$percentage
}
}
loop

There's a few other bits to the GUI that I've created but I've noticed one majour problem, and this goes for other GUI apps that I have created. Whenever the 'go' button is pressed to start the script from the dialog box, the GUI freezes. The menu bar that I've created inside the GUI freezes and no other button can be pressed.

Am I supposed to run each function in a different thread? Something like that?

Thanks

解决方案

Try to add:

[System.Windows.Forms.Application]::DoEvents() 

in your loop. Quick and dirty, however.

这篇关于Powershell:冻结GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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