You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While using RunCode in combination of Event handlers being invoked from background thread side effects are possible. In the example below exception will appear because context will be clear immediately after call to RunCode, but will be still referenced and used by the event handler which will be invoked afterwards as a result of async function execution:
[TestMethod]
public void ProblemWithGlobalScopeWithAsyncEvents() {
CustomerFacade rez = (CustomerFacade)Script.RunCode(@"
abc = '123';
function OnGetCustomersCompleted(s, e)
{
s.v = abc;
}
s = new CustomerFacade();
s.GetCustomersCompleted += OnGetCustomersCompleted;
s.GetCustomersAsync();
return s;
");
//Now scope is clear, while event is still subscribed and will be invoked
while (rez.busy) System.Threading.Thread.Sleep(100);
Assert.IsTrue(rez.v.Contains("given handler is not associated with any context"));
}
public class CustomerFacade {
public event EventHandler<EventArgs> GetCustomersCompleted;
public string v { get; set; }
public bool busy { get; set; }
public void GetCustomersAsync()
{
busy = true;
Task t = Task.Factory.StartNew(
() => {
System.Threading.Thread.Sleep(500);
try {
if (GetCustomersCompleted != null) {
GetCustomersCompleted.Invoke(this, EventArgs.Empty);
}
}
catch(Exception e)
{
v = e.ToString();
}
finally {
busy = false;
}
});
}
}
The text was updated successfully, but these errors were encountered:
While using RunCode in combination of Event handlers being invoked from background thread side effects are possible. In the example below exception will appear because context will be clear immediately after call to RunCode, but will be still referenced and used by the event handler which will be invoked afterwards as a result of async function execution:
}
The text was updated successfully, but these errors were encountered: