0

serverProtocolsManagement.Get()以下 WMI 查询在何时是无效的服务器名称时引发异常sqlHost(如预期的那样。但是,如果我尝试使用相同的无效参数再次调用此代码,则ManagementScope构造函数将无限期挂起(从不返回或引发错误,只是挂起)。对此有合理的解释吗?

try {

        ManagementScope managementScope = new ManagementScope(@"\\" + sqlHost + @"\root\Microsoft\SqlServer\ComputerManagement");
        using (ManagementClass serverProtocolsManagement = new ManagementClass(managementScope, new ManagementPath("ServerNetworkProtocol"), null)) {

            serverProtocolsManagement.Get();

            using (ManagementObjectCollection protocols = serverProtocolsManagement.GetInstances()) {
                foreach (ManagementObject protocol in protocols ) {
                    protocol.Get();

                    if ((string)protocol.GetPropertyValue("ProtocolName") == "Tcp" &&
                        (string)protocol.GetPropertyValue("InstanceName") == sqlInstanceName) {

                        protocol.InvokeMethod("SetEnable", null);
                    }
                }
            }
        }
    } catch (COMException ex) {
        MessageBox.Show(ex.ToString());
    }

编辑:

我尝试过使用不同的超时选项(通过将ConnectionOptions对象传递给ManagementScope构造函数),但无济于事。

编辑#2:

我不知道为什么我没有想到这一点(尽管它仍然不应该是一个问题):我在调试时从 VS 中的即时窗口调用此代码。一定存在某种线程问题,因为一旦我将此代码链接到 Windows 窗体按钮,一切正常。谢谢您的帮助!!

4

2 回答 2

0

当您使用 ManagementClass 构造函数(使用字符串范围而不是 ManagementScope 对象)时,您是否有同样的问题?

(因此跳过整个 ManagementScope 步骤)

于 2009-07-14T18:38:01.027 回答
0

显然,在调试时从即时窗口运行 WMI 查询是不行的。由于按下按钮而执行查询,而是解决了问题。

于 2009-09-08T00:47:07.697 回答