问题描述
感谢您在以下方面的支持:
我有函数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
解决方法
我管理了适用于我但需要增强的解决方案
简单地通过在两个函数之间来回触发直到破坏主要函数:0)..如下代码中的注释。
public static Functions__V51 _functions = new Functions__V51();
static void Main(string[] args)
{
try
{
Console.WriteLine("return Code = " + getCode(processURL_));
}
catch (Exception ex)
{
// to catch thrown exception raised by Process Time Monitoring
Console.WriteLine(ex.Message);
//throw;
}
}
public static string getCode(string _processURL_)
{
try
{
if (functions__.linkExists(_processURL_))
{
stopWatch.Start();
// to be fired in parallel to measure the time and kill getCode function in case exceeded certain threshold
_functions._ProcessTimeMonitoring();
// do Asynchronous job here ...
// do Asynchronous job here ...
// webdriver was used in these Asynchronous jobs !! so I used it to raise the the exception which will affecr function getCode !!
// simply by kill webdriver in function _ProcessTimeMonitoring_
// do Asynchronous job here ...
}
}
catch(exceptiopn ex)
{
}
}
//============== Another class of functions
class Functions__V51
{
public async void _ProcessTimeMonitoring()
{
_TimeisUp = false;
stopWatch.Start();
try
{
await Task.Factory.StartNew(() => {
while (stopWatch.ElapsedMilliseconds < 20000 && !_TimeisUp)
{
}
stopWatch.Stop();
_TimeisUp = true;
Console.WriteLine("Process Time is Up !!!!!!!!");
throw new TimeoutException("Time is Up !!"); //
});
}
catch (TimeoutException ex)
{
Console.WriteLine(ex.Message);
// kill webdriver *********
_Driver_.Close();
_Driver_.Dispose();
_Driver_.Quit();
//throw;
}
}
} // end of Class Functions