Environment.SetEnvironmentVariable需要很长时间才能在User或Machine级别设置变量 [英] Environment.SetEnvironmentVariable takes a long time to set a variable at User or Machine level

查看:195
本文介绍了Environment.SetEnvironmentVariable需要很长时间才能在User或Machine级别设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#(.NET 3.5)中的Environment.SetEnvironmentVariable方法调用:

I am using the Environment.SetEnvironmentVariable method call in C# (.NET 3.5) like this:


Environment.SetEnvironmentVariable(
environmentVariable,
value,
Machine
);

Environment.SetEnvironmentVariable( environmentVariable, value, "Machine" );

在几个测试系统(运行XP和Windows 7)都需要2秒钟以上。
计算出可能是因为:如果目标是用户或机器,其他应用程序通过Windows WM_SETTINGCHANGE消息通知设置操作。
有什么办法可以将此通知禁止其他应用程序,以便我的环境快速设置并返回..?

However this single call takes well over 2 seconds on several test systems (running both XP and Windows 7). I figured out that this might be because : "If target is User or Machine, other applications are notified of the set operation by a Windows WM_SETTINGCHANGE message." Is there any way to suppress this Notification to other applications so that my environment is set quickly and returns back..?

请注意,我有一个组件它设置了大约20个环境变量,如果我使用如上所述的功能,则需要大约一分钟才能完成该任务。

Note that I am having a component which sets around 20 environment variables and if I use the function as I have described above, it takes around a minute to finish that task.

请建议!!

推荐答案

如果您用反射器取消SetEnvironmentVariable,您将在方法底部看到WM_SETTINGCHANGE与Win32调用SendMessageTimeout。句柄是HWND_BROADCAST(0xffff),所以每个顶层窗口都会收到消息,超时设置为1000ms。根据msdn:

If you disasambly the SetEnvironmentVariable with reflector you will see at the bottom of the methode that the WM_SETTINGCHANGE with a Win32 call to SendMessageTimeout. The handle is HWND_BROADCAST (0xffff) so every top window gets the message and the timeout is set to 1000ms. According to msdn:


如果此参数是HWND_BROADCAST
((HWND)0xffff),则该消息将发送到
系统中的所有顶级窗口,
包括禁用或不可见
未知的窗口。函数不
返回,直到每个窗口定时
出来。因此,总等待时间
可以达到uTimeout
的值乘以顶级
窗口的数量。

If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows. The function does not return until each window has timed out. Therefore, the total wait time can be up to the value of uTimeout multiplied by the number of top-level windows.

但是fuFlags参数设置为0

But the fuFlags parameter is set to 0.


SMTO_NORMAL(0x0000):调用
线程不能阻止
处理其他请求,而
等待函数返回。

SMTO_NORMAL (0x0000): The calling thread is not prevented from processing other requests while waiting for the function to return.

SMTO_BLOCK(0x0001):阻止
调用线程处理任何
其他请求,直到函数
返回。

SMTO_BLOCK (0x0001): Prevents the calling thread from processing any other requests until the function returns.

我不知道是否功能是否阻塞。您可以尝试仅使用Win32设置变量,并在设置所有变量后才发送广播消息。或者您可以直接访问注册表。

I'm not sure if the functions blocks or not. You can try to set the variable with only Win32 and only send the broadcast message after you set all variables. Or you can directly access the registry.

这篇关于Environment.SetEnvironmentVariable需要很长时间才能在User或Machine级别设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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