How to guarantee exclusive access when modifying n

2019-07-03 19:06发布

I am using WMI's Win32_NetworkAdapterConfiguration class, EnableStatic method to set static IPs of a Loopback network adapter for testing. I noticed that the OS takes out a lock when there's a visible property sheet. When user dismisses the sheet with changes, things freeze up until the update has completed.

How can I ensure exclusive access between my program in C# and Windows UI?

1条回答
叛逆
2楼-- · 2019-07-03 19:41

I think the EnableStatic method of the Win32_NetworkAdapterConfiguration class already acquires an exclusive lock to change the network adapter settings. A microsoft knowledge base article lead me to this conclusion Microsoft KB. There is also a COM interface called INetCfgLock you could use to acquire an exclusive lock for changing network adapter settings. To acquire the lock use the INetCfgLock::AcquireWriteLock method.

BEGIN EDIT:

Here is a link to the project on codeproject which shows the use of the INetCfgLock COM interface and the INetCfgLock::AcquireWriteLock in C#.

END EDIT

Hope, this helps.

查看更多
登录 后发表回答