DownloadStringAsync (infinite loop in Release only)
-
Can someone tell me why there is an infinite loop below only in release mode? If I add a printf in the while loop, then the infinite loop disappears. for (long i=1; i<(signed)Expiry_URLs.size(); i++) DownloadStringInBackground2(gcnew System::String(Expiry_URLs[i].c_str())); Process_Straddle_View(T, G_OPT, Error_Code); //ASSUMES THAT NO OTHER DOWNLOAD FINISHED BEFORE THIS RUNS while (!!G_OPT.Running); void DownloadStringCallback2( System::Object^ sender, System::Net::DownloadStringCompletedEventArgs^ e ) { // If the request was not canceled and did not throw // an exception, display the resource. --G_OPT.Running; if ( !e->Cancelled && e->Error == nullptr ) { System::String^ textString = dynamic_cast(e->Result); System::Console::WriteLine( textString ); } } void DownloadStringInBackground2( System::String^ address ) { ++G_OPT.Running; System::Net::WebClient^ client = gcnew System::Net::WebClient; System::Uri ^uri = gcnew System::Uri(address); // Specify that the DownloadStringCallback2 method gets called // when the download completes. client->DownloadStringCompleted += gcnew System::Net::DownloadStringCompletedEventHandler( DownloadStringCallback2 ); client->DownloadStringAsync( uri ); }
-
Can someone tell me why there is an infinite loop below only in release mode? If I add a printf in the while loop, then the infinite loop disappears. for (long i=1; i<(signed)Expiry_URLs.size(); i++) DownloadStringInBackground2(gcnew System::String(Expiry_URLs[i].c_str())); Process_Straddle_View(T, G_OPT, Error_Code); //ASSUMES THAT NO OTHER DOWNLOAD FINISHED BEFORE THIS RUNS while (!!G_OPT.Running); void DownloadStringCallback2( System::Object^ sender, System::Net::DownloadStringCompletedEventArgs^ e ) { // If the request was not canceled and did not throw // an exception, display the resource. --G_OPT.Running; if ( !e->Cancelled && e->Error == nullptr ) { System::String^ textString = dynamic_cast(e->Result); System::Console::WriteLine( textString ); } } void DownloadStringInBackground2( System::String^ address ) { ++G_OPT.Running; System::Net::WebClient^ client = gcnew System::Net::WebClient; System::Uri ^uri = gcnew System::Uri(address); // Specify that the DownloadStringCallback2 method gets called // when the download completes. client->DownloadStringCompleted += gcnew System::Net::DownloadStringCompletedEventHandler( DownloadStringCallback2 ); client->DownloadStringAsync( uri ); }
You have an empty loop here, and it has 'eaten' all your processor in the release mode:
while (!!G_OPT.Running);
It's not a good way to do such loops for wait until other operation completes. In such cases synchronization must be used, like events, or AsyncCallbacks.See my article about Windows 7 Taskbar timer here on CodeProject