Re: Winforms, Shown Event and SetEnvironmentVariable



On Thu, 14 Feb 2008 13:50:18 -0800, Dilip <rdilipk@xxxxxxxxx> wrote:

[...]
In the Form.Shown event I set a couple of environment variables using
the standard SetEnvironmentVariable API like so:

Environment.SetEnvironmentVariable("someKey", somevalue,
EnvironmentVariableTarget.User);

I have 2 such calls. Amazingly it takes nearly 10 seconds for these
two calls to execute! [...]

Am I doing something wrong?

You mean, other than setting an environment variable? :)

I'm surprised it takes 10 seconds for the calls to execute. I'd agree that seems unreasonably long. However, I can easily reproduce the exact behavior (almost down to the time...11 seconds on my computer, though I did see it take "only" 8 seconds once).

Also surprising is that I wasn't able to find any mention of this issue using Google. I tried a few different searches and a quick look at the results turned up no other mentions of the delay. It makes me wonder if this is a new problem, possibly related to a recent Windows Update.

But why are you setting environment variables anyway? That's kind of an archaic thing to do. Do you have a requirement for compatibility with some older software that uses the variables?

As far as dealing with the issue: I think the best solution is "don't do that". That is, don't use environment variables if you don't absolutely have to. I think the alternative (short of figuring out why it takes so long and possibly finding an alternative) would be to move those operations out of the GUI thread. You can use the BackgroundWorker class to execute them using the thread pool and then notify your Form class when the operation is done (so that it can update itself as appropriate).

One thing I didn't try was using unmanaged code to set the environment variables. That's something you might try, in order to at least understand if the issue is something unique to .NET, or is a general problem with setting environment variables. And if it is significantly faster using unmanaged code, you might try using p/invoke with the unmanaged API to set the variables rather than going through .NET.

Pete
.



Relevant Pages

  • Re: Winforms, Shown Event and SetEnvironmentVariable
    ... In the Form.Shown event I set a couple of environment variables using ... the standard SetEnvironmentVariable API like so: ... Environment.SetEnvironmentVariable("someKey", somevalue, ... Because it takes 10 seconds for these calls to execute, ...
    (microsoft.public.dotnet.languages.csharp)
  • [NEWS] MacOS X TruBlueEnvironment Privilege Escalation Attack
    ... TruBlueEnvironment is part of the MacOS Classic Emulator. ... By setting certain environment variables, ... run automatically via cron. ... remove global execute permissions from the TruBlueEnvironment executable, ...
    (Securiteam)
  • Re: Korn and csh variables issue
    ... korn shell and C shell? ... You could define them as environment variables, and THEN execute the shell. ... setvar a 123 ...
    (comp.unix.shell)
  • Re: [LINUX] ENV VAR addresses
    ... Making some tries, on my ubuntu, i found out that it changes every ... time i execute the program. ... The C standard itself doesn't say much about environment variables. ... It defines the getenv() function, which returns a pointer to a string ...
    (comp.lang.c)
  • Re: set environment variable
    ... there are several informix instances... ... this based on a permission scenario, ... build the environment variables for the session as a script, ... environment variables the directory to go and the program to execute ...
    (comp.databases.informix)