AnsweredAssumed Answered

Silverlight Page Lifecycle Problems with Asyncronous Event Handler

Question asked by bperniciaro on Apr 14, 2015

In my web application, I have code in the Page_Init event() of my Page which checks some session variables to redirect users if a Session timeout has occurred.


In my Silverlight application I have a button event handler which executes some ESRI ArcGIS code asynchronously, and which configures an event handler which fires when the asynchronous call completes:


    QueryTask queryTask = new QueryTask(myLayer.Url + "/" + layerID);     queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryCountyTask_ExecuteCompleted);     queryTask.ExecuteAsync(query);


There is also a bit of code which calls a JavaScript function on-page to hide a Panel.  The inclusion of this code causes a full page postback.




My problem is that sometimes the queryCountyTask_ExecuteCompleted() Silvelright event fires before the Page_Init() Page event and sometimes it fires after.  In those cases where the Silverlight fires before the Page event, the Session state is empty, causing the user to be incorrectly redirect to a "your session has timed-out" page.  As long as the Silverlight event fires after the Page event, the Session variables are still present and everything works fine.


It looks like the placement of the Invoke method has no bearing on when the Page-level event is fired, so the order of events appears to be seemingly random.


Is there a way to order the events so as to avoid these race conditions?