Re: Winforms, Shown Event and SetEnvironmentVariable
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 14 Feb 2008 14:20:31 -0800
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
.
- Follow-Ups:
- References:
- Winforms, Shown Event and SetEnvironmentVariable
- From: Dilip
- Winforms, Shown Event and SetEnvironmentVariable
- Prev by Date: webbrowser zoom
- Next by Date: Forms Authentication
- Previous by thread: Re: Winforms, Shown Event and SetEnvironmentVariable
- Next by thread: Re: Winforms, Shown Event and SetEnvironmentVariable
- Index(es):
Relevant Pages
|