1

I appreciate your support in below:

I have function getCode(processURL_) , that is complex function and has many function works in the underlying of it, and consume time, so I want to monitor the execution time and stop it if it exceeds certain threshold of 20 sec for example..

I figured out how to put threshold for the execution time of the process by using parallel monitoring process , but:

1- The required here is to fire parallel elapsed time calculation, using "monitoring process" that works in parallel along with the this complex process and measure the elapsed time <<=== this is done...........

2- And if it exceeds the threshold, it will Throw Exception for "Time is up" <<=== this is done...........

3- Now I cannot catch the thrown exception for time is up ?? as it is thrown by parallel process ( Tired both to be form the same class or another class )

it could be thrown to the system by the function _ProcessTimeMonitoring from the function class sucessfully, and kill the app, but I need to throw the exception by the function ProcessTimeMonitoring and catch it in main class to kill function getCode(processURL) using this exception trick <<=====

, So please could anyone help for this exception handling across classes.

or anyway to break the main function if the parallel function time is up ??

Thanks in advance.

    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
kingbode
  • 131
  • 2
  • 8

1 Answers1

0

I managed solution that worked for me but need enhancement

simply by using firing back and forth between the two functions until breaking the main function :0) .. as per below comments in the code.

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
kingbode
  • 131
  • 2
  • 8