简体   繁体   English

为什么 IAsyncResult.AsyncWaitHandle.WaitOne 在 wifi 没有 inte.net 连接且移动数据已打开时返回 false? (XAMARIN)

[英]Why does IAsyncResult.AsyncWaitHandle.WaitOne return false when wifi has no internet connection and mobile data is turned on? (XAMARIN)

I'm trying to do a sync with my custom device via wifi connection.我正在尝试通过 wifi 连接与我的自定义设备同步。 Everything works fine when my mobile data is turned off, but when it's turned on it won't work (because my custom device has no connection to the inte.net).当我的移动数据关闭时,一切正常,但当它打开时,它将无法工作(因为我的自定义设备没有连接到 inte.net)。

This is where the problem happens:这是问题发生的地方:

stateSuccess = true;
tcpclnt = new TcpClient();
IAsyncResult ar = tcpclnt.BeginConnect(ip, port, new AsyncCallback(connectCallback), stateSuccess);
int timeout = 3000;
stateSuccess = ar.AsyncWaitHandle.WaitOne(timeout, false);

when I have mobile data turned on stateSuccess = false , but when mobile data is turned off stateSuccess = true .当我打开移动数据时stateSuccess = false ,但是当移动数据关闭时stateSuccess = true I'm always connected over wifi to my custom device.我总是通过 wifi 连接到我的自定义设备。

My connectCallback is:我的 connectCallback 是:

private void connectCallback(IAsyncResult ar)
        {
            var stateSuccess = (Boolean)ar.AsyncState;

            try
            {
                tcpclnt.EndConnect(ar);
            }
            catch (Exception exc)
            {
                //handle
            }

            try
            {
                if (tcpclnt != null && tcpclnt.Connected && stateSuccess)
                    return;

                tcpclnt.Close();
            }
            catch (Exception exc)
            {
                if (tcpclnt != null)
                    tcpclnt.Close();
            }
        }

I think the problem is somewhere in it chosing the wrong.network.我认为问题出在某个地方选择了错误的网络。 How can I force it to use wifi.network even thou the wifi has no inte.net connection?即使 wifi 没有 inte.net 连接,我如何强制它使用 wifi.network?

The devices do not care nor discriminate the lower-level base-band radio scanning (we're talking about telephony here), that is left to the actual radio firmware to deal with.这些设备不关心也不区分较低级别的基带无线电扫描(我们在这里谈论的是电话),这是留给实际的无线电固件来处理的。

If you dont want to use WIFI, turn it off.如果您不想使用 WIFI,请将其关闭。 But if you want to force use WIFI, just turn your cellular data off但是如果你想强制使用 WIFI,只需关闭你的蜂窝数据

Edit: I just found a workaround to this that works ONLY for Android: https://stackoverflow.com/a/56590566/11104068编辑:我刚刚找到了一个仅适用于 Android 的解决方法: https://stackoverflow.com/a/56590566/11104068

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IAsyncResult.AsyncWaitHandle.WaitOne()在回调之前完成 - IAsyncResult.AsyncWaitHandle.WaitOne() completes ahead of callback 如果侦听服务已关闭,为什么TcpClient.BeginConnect结果的AsyncWaitHandle.WaitOne不返回false? - Why doesn't TcpClient.BeginConnect result's AsyncWaitHandle.WaitOne return false if the listening service is down? IAsyncResult.IsCompleted,iftAR.AsyncWaitHandle.WaitOne和AsyncCallback的使用条件 - Usage criteria for IAsyncResult.IsCompleted, iftAR.AsyncWaitHandle.WaitOne and AsyncCallback IAsyncResult.AsyncWaitHandle的使用 - Use of IAsyncResult.AsyncWaitHandle AsyncWaitHandle.WaitOne的详细信息 - Details of AsyncWaitHandle.WaitOne 为什么Task实现IAsyncResult,但不包含AsyncWaitHandle成员? - How come Task implements IAsyncResult, but does not contain the AsyncWaitHandle member? 在非UI线程上调用AsyncWaitHandle.WaitOne时出现NotSupportedException - NotSupportedException when calling AsyncWaitHandle.WaitOne on non-UI Thread 移动闪光灯打开使用xamarin但它没有转动,为什么? - Mobile Flash Light Turn on with xamarin but it is not turned,Why? 以编程方式选择Internet连接(WiFi,移动宽带等) - Programmatically choosing the Internet connection (WiFi, mobile broadband,…) AsyncWaitHandle.WaitOne阻止CLR线程吗? 或者它是否创建了I / O完成端口? - Does AsyncWaitHandle.WaitOne block the CLR thread? Or does it create an I/O completion port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM