2012-02-15 68 views
3

我尝试编写通过套接字连接到服务器的应用程序。所有的工作都很好,但是当应用程序在锁屏下运行时,套接字无法连接(在锁屏被移除时它正在等待)。在Windows Phone上的锁定屏幕下连接套接字

设备连接到PC,所以无线网络不应该有电池节电

如何重现(下面的代码)的影响(自动关闭):

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分五十秒

+0

如果被激活,锁屏前,它保持开放的插座已经打开? – MrMDavidson 2012-02-15 23:42:14

+0

Nop,所有发送数据当屏幕锁定被移除时,会引发'Completed'事件。虽然锁定,但没有什么... – Ku6opr 2012-02-16 08:19:39

+0

如果您看[MSDN的Windows Phone的空闲检测](http://msdn.microsoft.com/zh-cn/library/ff941090%28v=vs.92%29.aspx )有一个Silverlight应用程序清单。该清单中的第7项是; “指出你的应用程序不应该执行新的网络请求和隔离存储操作。”这并没有明确说明,但我敢打赌,在锁定屏幕下所有网络连接都被禁用。尝试在锁之前打开连接,让它继续运行,并查看锁定/重新锁定后会发生的情况。 – MrMDavidson 2012-02-16 23:18:55

回答

1

套接字时锁定屏幕芒果被激活没有收到任何数据。 对此没有已知的解决方法。

在这里看到更多的信息:

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

+0

我看到了这个链接,但我希望有一些解决方法存在。无论如何,谢谢 – Ku6opr 2012-02-27 13:34:45

+0

如果解决方法存在,这将是很好的,我很想看到它... – Roboblob 2012-02-27 15:27:51