感谢您在以下方面的支持:
我有函数 getCode(processURL_) ,这是一个复杂的函数,它的底层有很多函数工作,并且消耗时间,所以我想监控执行时间并在它超过 20 秒的某个阈值时停止它。
我想出了如何通过使用并行监视进程来设置进程执行时间的阈值,但是:
1-这里需要的是触发并行经过时间计算,使用与这个复杂过程并行工作的“监控过程”并测量经过时间<<===完成............ ..
2-如果超过阈值,它将为“时间到了”抛出异常<<===完成............
3-现在我无法捕获抛出的异常,因为时间到了??因为它是由并行过程抛出的(厌倦了形成同一个类或另一个类)
它可以由函数类中的函数 _ProcessTimeMonitoring 成功抛出到系统,并终止应用程序,但我需要通过函数ProcessTimeMonitoring 抛出异常并在主类中捕获它以使用此异常技巧终止函数 getCode(processURL) <<=====
,所以请任何人都可以帮助跨类处理这种异常。
或者如果并行功能时间到了,无论如何要打破主要功能?
提前致谢。
public static Functions__V51 _functions = new Functions__V51();
static void Main(string[] args)
{
try
{
Console.WriteLine("\n\n\t return Code = " + getCode(processURL_));
}
catch (Exception _functions.Time_is_Up)
{
throw;
// to catch thrown exception raised by Process Time Monitoring
Console.WriteLine(_functions.Time_is_Up.Message);
//throw;
}
}
public static string getCode(string _processURL_)
{
// 4-when the url is down the script should return the value 3.
try
{
if (functions__.linkExists(_processURL_))
{
stopWatch.Start();
_functions._ProcessTimeMonitoring_();
// do Asynchronous job here ...
}
}
catch(exceptiopn ex)
{
}
}
//============== Another class of functions
class Functions__V51
{
public Exception _Time_is_Up = new Exception("Time is UP, by the way");
public async void _ProcessTimeMonitoring()
{
_TimeisUp = false;
try
{
await Task.Factory.StartNew(() => {
while (stopWatch.ElapsedMilliseconds < 20000 && !_TimeisUp)
{
}
// raising killer flag for the main process
stopWatch.Stop();
_TimeisUp = true; // global bool flag
Console.WriteLine("Process Time is Up !!!!!!!!");
throw _Time_is_Up; // << this exception need to be caugh in main class to kill getCode function
});
}
catch (Exception _Time_is_Up)
{
//throw;
}
}
} // end of Class Functions