How to guarantee exclusive access when modifying n

2019-07-03 18:41发布

问题:

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:

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.