Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does setting a USER environment variable take 12 seconds?

Tags:

vbscript

qtp

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")

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.

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:

How can I set a USER environment variable in VBScript without getting the horrible runtime?

like image 698
TheBlastOne Avatar asked Nov 23 '25 02:11

TheBlastOne


1 Answers

This is a nice workaround (in C#):

var envName = "USERNAME";

var envValue = "JERRY";

// it's up to you if you await this or not
Task.Factory.StartNew(() => Environment.SetEnvironmentVariable(envName, envValue, EnvironmentVariableTarget.User));
like image 75
Jerry Nixon Avatar answered Nov 25 '25 00:11

Jerry Nixon