@@ -22,7 +22,9 @@ public static class CnCNetPlayerCountTask
2222 public static void InitializeService ( CancellationTokenSource cts )
2323 {
2424 cncnetLiveStatusIdentifier = ClientConfiguration . Instance . CnCNetLiveStatusIdentifier ;
25- PlayerCount = GetCnCNetPlayerCount ( ) ;
25+
26+ // This call is synchronous. Therefore, we use a short timeout to avoid blocking the main thread for too long.
27+ PlayerCount = GetCnCNetPlayerCount ( timeoutMilliseconds : 1000 ) ;
2628
2729 CnCNetGameCountUpdated ? . Invoke ( null , new PlayerCountEventArgs ( PlayerCount ) ) ;
2830 ThreadPool . QueueUserWorkItem ( new WaitCallback ( RunService ) , cts ) ;
@@ -41,12 +43,12 @@ private static void RunService(object tokenObj)
4143 }
4244 else
4345 {
44- CnCNetGameCountUpdated ? . Invoke ( null , new PlayerCountEventArgs ( GetCnCNetPlayerCount ( ) ) ) ;
46+ CnCNetGameCountUpdated ? . Invoke ( null , new PlayerCountEventArgs ( GetCnCNetPlayerCount ( timeoutMilliseconds : 5000 ) ) ) ;
4547 }
4648 }
4749 }
4850
49- private static int GetCnCNetPlayerCount ( )
51+ private static int GetCnCNetPlayerCount ( int timeoutMilliseconds = 5000 )
5052 {
5153 try
5254 {
@@ -56,7 +58,7 @@ private static int GetCnCNetPlayerCount()
5658 if ( string . IsNullOrWhiteSpace ( ClientConfiguration . Instance . CnCNetPlayerCountURL ) )
5759 return - 1 ;
5860
59- WebClient client = new ExtendedWebClient ( ) ;
61+ WebClient client = new ExtendedWebClient ( timeout : timeoutMilliseconds ) ;
6062 Stream data = client . OpenRead ( ClientConfiguration . Instance . CnCNetPlayerCountURL ) ;
6163
6264 string info = string . Empty ;
0 commit comments