我嘗試編寫通過套接字連接到服務器的應用程序。所有的工作都很好,但是當應用程序在鎖屏下運行時,套接字無法連接(在鎖屏被移除時它正在等待)。在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分五十秒
如果被激活,鎖屏前,它保持開放的插座已經打開? – MrMDavidson 2012-02-15 23:42:14
Nop,所有發送數據當屏幕鎖定被移除時,會引發'Completed'事件。雖然鎖定,但沒有什麼... – Ku6opr 2012-02-16 08:19:39
如果您看[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