在Windows Phone上的锁定屏幕下连接套接字

时间:2012-02-15 13:43:58

标签: windows-phone-7

我尝试编写连接到带有套接字的服务器的应用程序。一切正常,但当应用程序在锁定屏幕下运行时,套接字无法连接(它正在等待锁定屏幕被移除)。

设备连接到PC,因此WiFi不会对电池节省产生影响(自动关闭)

如何复制(下面的代码):

1)启动应用程序并等待30秒。在调试窗口中,您将看到:

Try to connect at 15:35:08
Connected at 15:35:08

2)启动应用程序,锁定屏幕并等待30秒。在调试窗口中,您将看到:

Try to connect at 15:36:07
Connected at 15:36:42

因此,在锁定屏幕期间没有任何反应,套接字被冻结

这是我的代码:

    public MainPage()
    {
        InitializeComponent();

        PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;

        DnsEndPoint dnsEndPoint = new DnsEndPoint("stackoverflow.com", 80);
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SocketAsyncEventArgs socketOperationEventArguments = new SocketAsyncEventArgs();
        socketOperationEventArguments.RemoteEndPoint = dnsEndPoint;
        socketOperationEventArguments.Completed += (s, e) =>
        {
            if (e.SocketError == SocketError.Success && e.ConnectSocket.Connected)
            {
                Debug.WriteLine("Connected at " + DateTime.Now.ToLongTimeString());
            }
        };

        DispatcherTimer Timer = new DispatcherTimer()
        {
            Interval = TimeSpan.FromSeconds(10)
        };
        Timer.Tick += (s, e) =>
        {
            Debug.WriteLine("Try to connect at " + DateTime.Now.ToLongTimeString());
            socket.ConnectAsync(socketOperationEventArguments);

            Timer.Stop();
        };
        Timer.Start();
    }

编辑:

发送数据也无法在锁定屏幕下工作。经度:

Try to connect at 10:18:39
Connected at 10:18:39
Try to send at 10:18:40
Send data at 10:18:40
Try to send at 10:18:41
Send data at 10:18:41
Try to send at 10:18:42
Send data at 10:18:42
Try to send at 10:18:43
Try to send at 10:18:44
Try to send at 10:18:45
Try to send at 10:18:46
Try to send at 10:18:47
Try to send at 10:18:48
Try to send at 10:18:49
Send data at 10:18:50
Send data at 10:18:50
Send data at 10:18:50
Send data at 10:18:50
Send data at 10:18:50
Send data at 10:18:50
Send data at 10:18:50
Try to send at 10:18:51
Send data at 10:18:51
Try to send at 10:18:52
Send data at 10:18:52

屏幕从10:18:43锁定到10:18:50

1 个答案:

答案 0 :(得分:1)

在Mango中激活锁定屏幕时,套接字未接收任何数据。 没有已知的解决方法。

在此处查看更多信息:

http://irc7.org/index.php/2011/09/07/why-doesnt-irc7-run-under-lock-screen/

你可以做的就是尽量防止锁定屏幕出现。 有关如何执行此操作的详细信息,请参阅此处:

http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.phoneapplicationservice.useridledetectionmode%28VS.92%29.aspx