为什么设置 USER 环境变量需要 12 秒? [英] Why does setting a USER environment variable take 12 seconds?

查看:41
本文介绍了为什么设置 USER 环境变量需要 12 秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我遇到了可怕的运行时:

With the following code, I experience horrible runtime:

Option Explicit

Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")
Dim Name: Name="MyVar"
Dim NewVal: NewVal="This is my value"

Services.StartTransaction "SetEnv"
ShellEnvironment (Name)=NewVal
Services.EndTransaction ("SetEnv")

请注意,只有 Services.* 内容是特定于 QTP 的.两条语句生成如下运行结果条目,表示环境变量赋值的运行时间:

Note that only the Services.* stuff is QTP-specific. The two statements generate the following run result entry, indicating the runtime for the environment variable assignment:

Transaction "SetEnv" ended with "Pass" status (Total Duration: 12.1970 sec). 

这是在一台非常快的机器上.当然,这是一个不可接受的长时间运行.

This is on a very fast machine. Of course it is an unacceptable long runtime.

根据环境.SetEnvironmentVariable 在用户或机器级别设置变量需要很长时间,这是因为所有顶级窗口都会收到 1 秒超时通知.我不确定这是否是 C# 特定的.嗯,显然不是.但我不知道如何在 VBScript 下控制此通知/超时过程.

According to Environment.SetEnvironmentVariable takes a long time to set a variable at User or Machine level, this is because all top-level windows are notified with a 1-second timeout. I am not sure if that is C#-specific or not. Well, it obviously is not. But I don´t see how I can control this notification/timeout process under VBScript.

所以一般来说,问题是:

So generally speaking, the question is:

如何在 VBScript 中设置 USER 环境变量而不会出现可怕的运行时?

推荐答案

似乎没有办法控制 ShellEnvironment 用于通知广播的超时.

所以我决定建立一个解决方法.

So I decided to build a workaround.

我称这个脚本为:

Option Explicit

Dim Name
Dim NewVal

If WScript.Arguments.Count <> 2 then
    WScript.Echo "setUSEREnv.vbs: Pass name and value of USER environment variable to set on the command line"
    WScript.Quit (1)
End If  

Name=CStr (WScript.Arguments.Item (0))
NewVal=CStr (WScript.Arguments.Item (1))

Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")
If ShellEnvironment (Name) <> NewVal then
    ShellEnvironment (Name)=NewVal
    WScript.Echo "Setting USER env var '" & Name & "'..."
    WScript.Echo "USER env var '" & Name & "' set to '" & NewVal & "'"
else
    WScript.Echo "USER env var '" & Name & "' already is '" & NewVal & "'"  
End If

从我使用 Run 的 setenv 函数代码:

from my setenv function code using Run:

Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")

Dim Name: Name=EnvVarName
If ShellEnvironment (Name) <> NewVal Then
    Dim Shell: Set Shell=CreateObject ("WScript.Shell")
    Shell.Run "cscript.exe ""c:\mydir\setUSERENV.vbs"" " & Name & " " & CStr (NewVal), 0, false
End If

对于每个 env var 写访问,这仍然让我感到很长时间的延迟,但至少主脚本继续运行,所以我不必等待.

This still insults me with a long delay for every env var write access, but at least the main script keeps running, and so I don´t have to wait.

这篇关于为什么设置 USER 环境变量需要 12 秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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