/// <summary> /// Connects (associates) to the specified wireless network, returning either on a success to connect /// or a failure. /// </summary> /// <param name="connectionMode"></param> /// <param name="bssType"></param> /// <param name="profile"></param> /// <param name="connectTimeout"></param> /// <returns></returns> public bool ConnectSynchronously(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. Stopwatch sw = new Stopwatch(); try { Connect(connectionMode, bssType, profile); sw.Start(); while (queueEvents && eventQueueFilled.WaitOne(connectTimeout, true)) { lock (eventQueue) { while (eventQueue.Count != 0) { if (sw.ElapsedMilliseconds > connectTimeout) { throw new Exception("Timeout"); } object e = eventQueue.Dequeue(); if (e is WlanConnectionNotificationEventData) { WlanConnectionNotificationEventData wlanConnectionData = (WlanConnectionNotificationEventData)e; // Check if the conditions are good to indicate either success or failure. if (wlanConnectionData.notifyData.notificationSource == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.notifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.connNotifyData.profileName == profile) { return(true); } break; } } break; } } } } } finally { sw.Stop(); queueEvents = false; eventQueue.Clear(); } return(false); // timeout expired and no "connection complete" }
/// <summary> /// Connects (associates) to the specified wireless network, returning either on a success to connect /// or a failure. /// </summary> /// <param name="connectionMode"></param> /// <param name="bssType"></param> /// <param name="profile"></param> /// <param name="connectTimeout"></param> /// <returns></returns> public bool ConnectSynchronously(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { _queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. try { Connect(connectionMode, bssType, profile); while (_queueEvents && _eventQueueFilled.WaitOne(connectTimeout, true)) { lock (_eventQueue) { while (_eventQueue.Count != 0) { object e = _eventQueue.Dequeue(); if (!(e is WlanConnectionNotificationEventData)) { continue; } WlanConnectionNotificationEventData wlanConnectionData = (WlanConnectionNotificationEventData)e; // Check if the conditions are good to indicate either success or failure. if (wlanConnectionData.NotifyData.notificationSource == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.NotifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.ConnNotifyData.profileName == profile) { return(true); } break; } } break; } } } } finally { _queueEvents = false; lock (_eventQueue) { _eventQueue.Clear(); } } return(false); // timeout expired and no "connection complete" }
/// <summary> /// Connects to the specified wireless network. /// </summary> /// <remarks> /// The method returns immediately. Progress is reported through the <see cref="WlanNotification"/> event. /// </remarks> public void Connect(WlanConnectionMode connectionMode, Dot11BssType bssType, Dot11Ssid ssid, WlanConnectionFlags flags) { WlanConnectionParameters connectionParams = new WlanConnectionParameters(); connectionParams.wlanConnectionMode = connectionMode; connectionParams.dot11SsidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid)); Marshal.StructureToPtr(ssid, connectionParams.dot11SsidPtr, false); connectionParams.dot11BssType = bssType; connectionParams.flags = flags; Connect(connectionParams); Marshal.DestroyStructure(connectionParams.dot11SsidPtr, ssid.GetType()); Marshal.FreeHGlobal(connectionParams.dot11SsidPtr); }
/// <summary> /// Connects (associates) to the specified wireless network, returning either on a success to connect /// or a failure. /// </summary> /// <param name="connectionMode"></param> /// <param name="bssType"></param> /// <param name="profile"></param> /// <param name="connectTimeout"></param> /// <returns></returns> public bool ConnectSynchronously(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. try { Connect(connectionMode, bssType, profile); while (queueEvents && eventQueueFilled.WaitOne(connectTimeout, true)) { lock (eventQueue) { while (eventQueue.Count != 0) { object e = eventQueue.Dequeue(); if (e is WlanConnectionNotificationEventData) { WlanConnectionNotificationEventData wlanConnectionData = (WlanConnectionNotificationEventData)e; // Check if the conditions are good to indicate either success or failure. if (wlanConnectionData.notifyData.notificationSource == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.notifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.connNotifyData.profileName == profile) return true; break; } } break; } } } } } finally { queueEvents = false; eventQueue.Clear(); } return false; // timeout expired and no "connection complete" }
/// <summary> /// Requests a connection (association) to the specified wireless network. /// </summary> /// <remarks> /// The method returns immediately. Progress is reported through the <see cref="WlanNotification"/> event. /// </remarks> public void Connect(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile) { WlanConnectionParameters connectionParams = new WlanConnectionParameters(); connectionParams.wlanConnectionMode = connectionMode; connectionParams.profile = profile; connectionParams.dot11BssType = bssType; connectionParams.flags = 0; Connect(connectionParams); }