Environment.SetEnvironmentVariable需要很长的时间在用户或机器级设置一个变量 [英] Environment.SetEnvironmentVariable takes a long time to set a variable at User or Machine level

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

问题描述

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

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

Environment.SetEnvironmentVariable(
environmentVariable,
值,

);

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.

请建议!!

推荐答案

如果您disasambly与反射器SetEnvironmentVariable方法,你将在梅索德的底部看到,随着一个Win32调用SendMessageTimeout的WM_SETTINGCHANGE。手柄是HWND_BROADCAST(0xFFFF的),所以每一个顶部窗口获取消息和超时被设置为1000毫秒。根据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的),消息被发送到
。在系统中,
包括残疾或不可见
无主窗口的所有顶级窗口。该功能不
返回,直到每个窗口已超时
OUT。因此,总的等待时间
可高达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(0×0000):调用$ b $ 。而
等待返回功能并不从
处理其他请求防止b导线

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

SMTO_BLOCK(0×0001):防止所述
从处理任何
其他请求,直到函数
返回调用线程。

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需要很长的时间在用户或机器级设置一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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