Why the objects do not get disposed when the scope ends while using Delegates

I have come across a weird scenario where the execution of a delegate continues even after the scope ends. I have also used GC.collect() at last but even it didn’t work out.

I was using something related to selenium which keep executing till last, But You can print count.

Here I want to know the reason that why It keeps on executing the code even when it goes out of scope

class OptimixAutomation
        {
            public delegate void DelAutomationAction(ExeActions actionId);
            static void Main(string[] args)
            {
                int numberOfInstances = Convert.ToInt32(ConfigurationManager.AppSettings["NumberOfInstances"]);
                ExeActions actionId = (ExeActions)Convert.ToInt32(ConfigurationManager.AppSettings["ActionId"]);
                for (int i = 0; i < numberOfInstances; i++)
                {
                    AutomationActions automationActions = new AutomationActions();
                    DelAutomationAction delAutomationAction = new DelAutomationAction(automationActions.Login);       
                    try
                    {
                        delAutomationAction.BeginInvoke(actionId: actionId, callback: null, @object: null);
                    }
                    catch (Exception e)
                    {

                    }
                    Thread.Sleep(10000);
                }
                Console.ReadKey();
            }

        }


Source: .net

Leave a Reply