加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Environment.SetEnvironmentVariable需要很长时间才能在用

发布时间:2020-12-15 08:37:32 所属栏目:百科 来源:网络整理
导读:我正在使用C#(.NET 3.5)中的Environment.SetEnvironmentVariable方法调用,如下所示: Environment.SetEnvironmentVariable( environmentVariable, value, “Machine” ); 但是,在单个测试系统(运行XP和Windows 7)上,这个单个调用需要2秒多的时间. I figured
我正在使用C#(.NET 3.5)中的Environment.SetEnvironmentVariable方法调用,如下所示:

Environment.SetEnvironmentVariable(
environmentVariable,
value,
“Machine”
);

但是,在单个测试系统(运行XP和Windows 7)上,这个单个调用需要2秒多的时间.
I figured out,这可能是因为:“如果目标是User或Machine,则通过Windows WM_SETTINGCHANGE消息通知其他应用程序设置操作.”
有没有办法将此通知抑制到其他应用程序,以便我的环境快速设置并返回..?

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

请建议!!

解决方法

如果您使用反射器解除SetEnvironmentVariable的设置,您将在方法的底部看到WM_SETTINGCHANGE,其中Win32调用SendMessageTimeout.句柄是HWND_BROADCAST(0xffff),因此每个顶部窗口都获取消息,超时设置为1000ms.根据msdn:

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.

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

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

我不确定函数是否阻塞.您可以尝试仅使用Win32设置变量,并仅在设置所有变量后发送广播消息.或者您可以直接访问注册表.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读