2012-02-15 67 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