-1

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. Here I meant automationActions, delAutomationAction go 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();
    }
}
Ajay Suwalka
  • 549
  • 1
  • 7
  • 24
  • 1
    When what goes out of scope? `BeginInvoke` will execute your delegate on asynchronously on a threadpool thread - this is likely where your confusion is arising. See the answer to [this question](http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke) for more details. – Charles Mager Jul 08 '15 at 08:46
  • @CharlesMager Thank you for the reply, Here I meant automationActions, delAutomationAction go out of scope . – Ajay Suwalka Jul 08 '15 at 11:29
  • Please remove your `try` / `catch (Exception e)` code. It's an awful practice to swallow exceptions like this. You really should never even just do `catch (Exception e)`. It may not make a difference to this issue, but it may save you hours of debugging time elsewhere. – Enigmativity Jul 08 '15 at 12:26
  • For starters you have a memory leak as you never called `EndInvoke`... – leppie Jul 08 '15 at 12:36
  • @Enigmativity, Since I am not worried about catching it directly. Since My program shouldn't crash in case of crashing a thread. Thank you all for your valuable comments but my question is still unanswered – Ajay Suwalka Jul 09 '15 at 13:59

1 Answers1

-1

AutomationActions automationActions = new AutomationActions(); DelAutomationAction delAutomationAction = new DelAutomationAction(automationActions.Login); - This line. As you can see, DelAutomationAction have reference to automationActions. Because DelAutomationAction is executed asynchronously, so it's still in "scope". And because GC can get to "automationActions" from "delAutomationAction", the first object won't be removed(and disposed).

Warzyw
  • 329
  • 3
  • 11