Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- RunDirectly();
- RunIndirectly();
- var sw = new Stopwatch();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunDirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunIndirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunIndirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunDirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunDirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunIndirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunIndirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- sw.Start();
- for (int i = 0; i < 100; i++)
- {
- RunDirectly();
- }
- sw.Stop();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}");
- sw.Reset();
- }
- private void RunDirectly()
- {
- var logger = new Logger();
- try
- {
- SomeMethod();
- logger.Write("Completed Without Exception");
- return;
- }
- catch (Exception ex)
- {
- logger.Write("Exception During Crm Service Operation");
- logger.Write(ex);
- throw;
- }
- }
- private void RunIndirectly()
- {
- var logger = new Logger();
- Run(SomeMethod,logger);
- }
- private void SomeMethod()
- {
- SomeMethod(20);
- }
- private void SomeMethod(int milliSeconds)
- {
- Thread.Sleep(milliSeconds);
- }
- private void Run(Action method, Logger _logger)
- {
- Run<object>(() =>
- {
- method();
- return null;
- }, _logger);
- }
- // Define other methods and classes here
- private T Run<T>(Func<T> method, Logger _logger)
- {
- if (method == null)
- {
- _logger.Write($"Null Argument: {nameof(method)}");
- throw new ArgumentNullException(nameof(method));
- }
- try
- {
- var result = method();
- return _logger.WriteAndReturn(result, "Completed Without Exception");
- }
- catch (Exception ex)
- {
- _logger.Write("Exception During Crm Service Operation");
- _logger.Write(ex);
- throw;
- }
- }
- public class Logger
- {
- public T WriteAndReturn<T>(T returnObject, string contents)
- {
- return returnObject;
- }
- public void Write(string contents)
- {}
- public void Write(Exception contents)
- {}
- }
Add Comment
Please, Sign In to add comment