c# - Why the objects do not get disposed when the scope ends while using Delegates -
i have come across weird scenario execution of delegate continues after scope ends. have used gc.collect() @ last didn't work out.
i using related selenium keep executing till last, can print count.
here want know reason why keeps on executing code when goes out of scope. here 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"]); (int = 0; < 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(); } }
automationactions automationactions = new automationactions(); delautomationaction delautomationaction = new delautomationaction(automationactions.login); - line. can see, delautomationaction have reference automationactions. because delautomationaction executed asynchronously, it's still in "scope". , because gc can "automationactions" "delautomationaction", first object won't removed(and disposed).
Comments
Post a Comment