关闭使用托管API的WiFi连接 [英] Closing WiFi connections using the Managed API

查看:678
本文介绍了关闭使用托管API的WiFi连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用href=\"http://managedwifi.codeplex.com/\" rel=\"nofollow\">托管的WiFi API 的

I'm writing a program using the Managed WiFi API. Here's how I get all the networks in range:

    void UpdateNetworks()
    {
        networks = new List<Wlan.WlanAvailableNetwork>();
        WlanClient client = new WlanClient();
        foreach(WlanClient.WlanInterface iface in client.Interfaces)
        {
            Wlan.WlanAvailableNetwork[] nets = iface.GetAvailableNetworkList(0);
            foreach(Wlan.WlanAvailableNetwork net in nets)
                networks.Add(net);
        }
    }



的问题是,经过18调用该方法我可以不再连接:

The problem is that after 18 calls to this method I can no longer connect:

(0X80004005):尝试以
制成建立会话的网络
。服务器,但目前已经建立了该服务器太多
会议

(0x80004005): An attempt was made to establish a session to a network server, but there are already too many sessions established to that server.

下面是一个的抛出异常的构造器:

Here's the constructor that's throwing the exception:

    public WlanClient()
    {
        Wlan.ThrowIfError(
            Wlan.WlanOpenHandle(Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle));
        try
        {
            Wlan.WlanNotificationSource prevSrc;
            wlanNotificationCallback = new Wlan.WlanNotificationCallbackDelegate(OnWlanNotification);
            Wlan.ThrowIfError(
                Wlan.WlanRegisterNotification(clientHandle, Wlan.WlanNotificationSource.All, false, wlanNotificationCallback, IntPtr.Zero, IntPtr.Zero, out prevSrc));
        }
        catch
        {
            Wlan.WlanCloseHandle(clientHandle, IntPtr.Zero);
            throw;
        }
    }



我相信这是因为客户端永远不会关闭连接它打开。我该如何明确地关闭它们?有处理的块关闭,但它需要访问客户的私处。

I believe this is because the client never closes the connections it opens. How do I close them explicitly? There's handle closing in the catch block, but it requires access to the client's private parts.

推荐答案

既然你只有在一定次数的迭代后,看不到的问题,这个问题是某种,这听起来像资源及时没有得到清理。对可能资源枯竭

Since you're seeing problems only after a certain number of iterations, the problem is likely resource exhaustion of some sort, which sounds like resources aren't getting cleaned up in a timely manner.

从上面的意见,这听起来像你不处理你的 WlanClient 的情况下,这可能是部分(或全部)问题。我能理解你为什么不处理他们,虽然,因为他们不给你任何明显的方式这样做。这似乎是在他们的部分真的有问题的设计。有迹象表明,说像这样的类应该给你是公的Dispose 方法或各种设计指导公共关闭方法,但即使他们的两个的那些方法,他们故意让他们私人。

From the comments above, it sounds like you're not disposing your WlanClient instances, which may be part (or all) of the problem. I can understand why you're not disposing them, though, because they don't give you any obvious way to do so. This seems like a really problematic design on their part. There are all kinds of design guidelines that say a class like this should give you either a public Dispose method or a public Close method, but even though they have both those methods, they deliberately made them both private.

但类实现了 IDisposable的,所以你仍然可以清理加入了使用块:

But the class does implement IDisposable, so you can still clean it up by adding a using block:

using (var wlanClient = new WlanClient()) {
    ....
} // wlanClient will be disposed when flow leaves the block

这将确保该对象的所有资源得到此刻流清理树叶使用块(即使流动离开,因为有一个例外)。您的连接将被关闭,你的非托管内存释放出来,和其他任何需要做的。

This will make sure all of the object's resources get cleaned up at the moment flow leaves the using block (even if flow is leaving because there was an exception). Your connections will be closed, your unmanaged memory released, and whatever else needs to happen.

这篇关于关闭使用托管API的WiFi连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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