Originally posted by: OptimisationSpecialist
We have some C# code which stops PrintConflict() after a specified time limit (i.e. the file is too big). The only problem is that the program hangs (it works fine if PrintConflict is small and completes). We are using a SOAP Webservice and Internet Information Services. I am building my knowledge about this configuration.
Is it possible to stop PrintConflict programmatically without hanging the application? What is the recommended approach?
I know that Thread.Suspend() is obsolete. The code below was provided by a contractor.
else
{
//printConflictDelegate = new PrintConflictDelegate(runConfig.GetOplModel().PrintConflict);
//printConflictDelegate.BeginInvoke(writeConflictFile);
System.Threading.Thread printConflictThread = new System.Threading.Thread(() => runConfig.GetOplModel().PrintConflict(writeConflictFile));
printConflictThread.Start();
while (!printConflictThread.IsAlive) ;//wait until printConflictThread commenced
AddDetailToProcessLog(System.Reflection.MethodBase.GetCurrentMethod().Name, "Printing conflict file with time limit of " + printConflictFileTimeLimit + " seconds");
for (int sec = 0; sec < printConflictFileTimeLimit; sec++)
{
System.Threading.Thread.Sleep(1000); //sleep with a limit
if (!printConflictThread.IsAlive) //back to work if printConflictThread is done
{
AddDetailToProcessLog(System.Reflection.MethodBase.GetCurrentMethod().Name, "Conflict file printing finished ahead from the time limit");
break;
}
}
if (printConflictThread.IsAlive)//if the printConflictThread is not yet done but sleep limit is reached... abort!
{
AddDetailToProcessLog(System.Reflection.MethodBase.GetCurrentMethod().Name, "Conflict file printing reached the time limit");
AddDetailToProcessLog(System.Reflection.MethodBase.GetCurrentMethod().Name, "Forcing the thread to shutdown. This will throw an error.");
AddDetailToProcessLog(System.Reflection.MethodBase.GetCurrentMethod().Name, "Process Exited");
printConflictThread.Suspend();
}
writeConflictFile.Flush();
writeConflictFile.Close();
writeConflictFile = null;
}
#endregion thread branch
#DecisionOptimization#OPLusingCPLEXOptimizer