1

感谢您在以下方面的支持:

我有函数 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
4

1 回答 1

0

我管理了对我有用但需要改进的解决方案

只需在两个函数之间来回触发,直到破坏主函数: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
于 2020-08-17T21:22:55.353 回答