C# 如何在c中控制进程的执行时间并跨类处理异常#
我感谢您在以下方面的支持: 我有函数getCode(processURL_),这是一个复杂的函数,在它的底层有许多函数工作,并且会消耗时间,因此我想监控执行时间,并在它超过某个阈值(例如20秒)时停止它 我通过使用并行监视进程,找到了如何设置进程执行时间的阈值,但是:C# 如何在c中控制进程的执行时间并跨类处理异常#,c#,exception,execution-time,performance-measuring,C#,Exception,Execution Time,Performance Measuring,我感谢您在以下方面的支持: 我有函数getCode(processURL_),这是一个复杂的函数,在它的底层有许多函数工作,并且会消耗时间,因此我想监控执行时间,并在它超过某个阈值(例如20秒)时停止它 我通过使用并行监视进程,找到了如何设置进程执行时间的阈值,但是: 1-这里需要的是启动并行运行时间计算,使用和这个复杂过程并行工作的“监控过程”,并测量运行时间我管理的解决方案,该解决方案对我有效,但需要增强 只需在两个函数之间来回触发,直到中断主函数:0)。。根据以下代码中的注释 public
1-这里需要的是启动并行运行时间计算,使用和这个复杂过程并行工作的“监控过程”,并测量运行时间我管理的解决方案,该解决方案对我有效,但需要增强 只需在两个函数之间来回触发,直到中断主函数: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
publicstaticfunctions\uuuv51\u Functions=newfunctions\uuuuv51();
静态void Main(字符串[]参数)
{
尝试
{
Console.WriteLine(“返回代码=“+getCode(processURL)”);
}
捕获(例外情况除外)
{
//捕获进程时间监视引发的抛出异常
控制台写入线(例如消息);
//投掷;
}
}
公共静态字符串getCode(字符串\u进程URL\u)
{
尝试
{
if(函数链接存在(_进程URL))
{
秒表。开始();
//在超过某个阈值时,并行触发以测量时间并终止getCode函数
_函数。_ProcessTimeMonitoring();
//在这里做异步工作。。。
//在这里做异步工作。。。
//这些异步作业中使用了webdriver!!所以我用它引发异常,这将影响函数getCode!!
//只需在函数\u ProcessTimeMonitoring中杀死webdriver_
//在这里做异步工作。。。
}
}
渔获物(例外)
{
}
}
//================另一类函数
类函数\uuuv51
{
公共异步void_ProcessTimeMonitoring()
{
_TimeisUp=false;
秒表。开始();
尝试
{
等待任务。工厂。开始新建(()=>{
同时(stopWatch.ElapsedMilliseconds<20000&!\u TimeisUp)
{
}
秒表;
_TimeisUp=true;
控制台。WriteLine(“处理时间到了!!!!!!!”;
抛出新的TimeoutException(“时间到了!!”)//
});
}
捕获(TimeoutException例外)
{
控制台写入线(例如消息);
//杀死网络驱动程序*********
_驱动程序关闭();
_驱动程序.Dispose();
_驱动程序退出();
//投掷;
}
}
}//类结束函数
我管理的解决方案适合我,但需要改进
只需在两个函数之间来回触发,直到中断主函数: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
publicstaticfunctions\uuuv51\u Functions=newfunctions\uuuuv51();
静态void Main(字符串[]参数)
{
尝试
{
Console.WriteLine(“返回代码=“+getCode(processURL)”);
}
捕获(例外情况除外)
{
//捕获进程时间监视引发的抛出异常
控制台写入线(例如消息);
//投掷;
}
}
公共静态字符串getCode(字符串\u进程URL\u)
{
尝试
{
if(函数链接存在(_进程URL))
{
秒表。开始();
//在超过某个阈值时,并行触发以测量时间并终止getCode函数
_函数。_ProcessTimeMonitoring();
//在这里做异步工作。。。
//在这里做异步工作。。。
//这些异步作业中使用了webdriver!!所以我用它引发异常,这将影响函数getCode!!
//只需在函数\u ProcessTimeMonitoring中杀死webdriver_
//在这里做异步工作。。。
}
}
渔获物(例外)
{
}
}
//================另一类函数
类函数\uuuv51
{
公共异步void_ProcessTimeMonitoring()
{
_TimeisUp=false;
秒表。开始();
尝试
{
等待任务。工厂。开始新建(()=>{
同时(stopWatch.ElapsedMilliseconds<20000&!\u TimeisUp)
{
}
秒表;
_TimeisUp=true;
控制台。WriteLine(“处理时间到了!!!!!!!”;
抛出新的TimeoutException(“时间到了!!”)//
});
}
捕获(TimeoutException例外)
{
控制台写入线(例如消息);
//杀死网络驱动程序*********
_驱动程序关闭();
_驱动程序.Dispose();
_驱动程序退出();
//投掷;
}
}
}//类结束函数