11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . ComponentModel ;
45using System . Diagnostics ;
@@ -394,8 +395,8 @@ protected override void OnLoad(EventArgs e)
394395 }
395396 Program . FileSystem . AppendToLogFile ( "Game window initialised successfully." ) ;
396397 //Initialise the loader thread queues
397- jobs = new Queue < ThreadStart > ( 10 ) ;
398- locks = new Queue < object > ( 10 ) ;
398+ jobs = new ConcurrentQueue < ThreadStart > ( ) ;
399+ locks = new ConcurrentQueue < object > ( ) ;
399400 Program . Renderer . Initialize ( ) ;
400401 Program . Renderer . DetermineMaxAFLevel ( ) ;
401402 Interface . CurrentOptions . Save ( OpenBveApi . Path . CombineFile ( Program . FileSystem . SettingsFolder , "1.5.0/options.cfg" ) ) ;
@@ -1126,18 +1127,15 @@ public void LoadingScreenLoop()
11261127
11271128 if ( Loading . JobAvailable )
11281129 {
1129- while ( jobs . Count > 0 )
1130+ while ( ! jobs . IsEmpty )
11301131 {
1131- lock ( jobLock )
1132+ jobs . TryDequeue ( out ThreadStart currentJob ) ;
1133+ currentJob ( ) ;
1134+ lock ( currentJob )
11321135 {
1133- var currentJob = jobs . Dequeue ( ) ;
1134- var locker = locks . Dequeue ( ) ;
1135- currentJob ( ) ;
1136- lock ( locker )
1137- {
1138- Monitor . Pulse ( locker ) ;
1139- }
1136+ Monitor . Pulse ( currentJob ) ;
11401137 }
1138+
11411139 }
11421140 Loading . JobAvailable = false ;
11431141 }
@@ -1156,28 +1154,22 @@ public void LoadingScreenLoop()
11561154 }
11571155 }
11581156
1159- private static readonly object jobLock = new object ( ) ;
1160- private static Queue < ThreadStart > jobs ;
1161- private static Queue < object > locks ;
1157+ private static ConcurrentQueue < ThreadStart > jobs ;
11621158
11631159 /// <summary>This method is used during loading to run commands requiring an OpenGL context in the main render loop</summary>
11641160 /// <param name="job">The OpenGL command</param>
11651161 /// <param name="timeout">The timeout</param>
11661162 internal static void RunInRenderThread ( ThreadStart job , int timeout )
11671163 {
11681164 object locker = new object ( ) ;
1169- lock ( jobLock )
1170- {
1171- jobs . Enqueue ( job ) ;
1172- locks . Enqueue ( locker ) ;
1173- //Don't set the job to available until after it's been loaded into the queue
1174- Loading . JobAvailable = true ;
1175- }
1176- lock ( locker )
1177- {
1178- //Failsafe: If our job has taken more than the timeout, stop waiting for it
1179- //A missing texture is probably better than an infinite loadscreen
1180- Monitor . Wait ( locker , timeout ) ;
1165+ jobs . Enqueue ( job ) ;
1166+ //Don't set the job to available until after it's been loaded into the queue
1167+ Loading . JobAvailable = true ;
1168+ //Failsafe: If our job has taken more than the timeout, stop waiting for it
1169+ //A missing texture is probably better than an infinite loadscreen
1170+ lock ( job )
1171+ {
1172+ Monitor . Wait ( job , timeout ) ;
11811173 }
11821174 }
11831175 }
0 commit comments