Click here to Skip to main content
15,881,803 members
Articles / Silverlight / Silverlight4

Doesn’t Your Girlfriend Deserve More Time Rather than Silverlight ??

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Apr 2011CPOL2 min read 5.7K  
Asynchronous programming model in Silverlight 4 with WCF RIA

Understanding Asynchronous Programming Model in Silverlight 4 with WCF RIA

Welcome friends! Although it seems like a tabloid heading, the experience with Silverlight can be troublesome and frustrating if you ignore the async programming model/calls which are used predominantly in Silverlight and WCF RIA. The Async model changes logic and flow of code creating confusion until you know about it.

So this post is a must if you are a newbie to Silverlight with RIA services and want to spend a little time searching through forums/blog for some simple but crazy results.

Let's start with an example scenario, a typical issue where the listbox didn’t update although we used to re-fetch the entity collection after adding some entity, using domainservicecontext.

The Issue and Scenario

Let's be clear that this is not exactly an issue but this is the way the RIA asynchronous programming works. Consider a case as below:

SNAGHTML9622ed

Now not so difficult but surely you are going to pass through a frustrating stage if you ignore the rest of the article.

The Normal way of Doing It

Normally once the popup closed, we are reloading the collection and binding to the list.

C#
private void btnAddNew_Click(object sender, RoutedEventArgs e) 
{ 
//Create a New State Object 
State stateObject = new State(); 
//Add to the StateCollection of Domain DataContext 
dataContext.States.Add(stateObject); 
//Create a PopUp Child Window Object 
Views.AddNewState chldWind = new Views.AddNewState(); 
//Getting the GridLayout Control of the popUp To Be Shown and assign the Newly 
//Created Object 
((Grid)chldWind.FindName("chldMainContainer")).DataContext = stateObject; 
//Attach The Todo on Close Event handler 
chldWind.Closed += new EventHandler(chldWind_Closed); 
chldWind.Show(); 
} 
void chldWind_Closed(object sender, EventArgs e) 
{ 
ChildWindow chldWind = (ChildWindow)sender; 
if ((chldWind.DialogResult == true)) 
{ 
dataContext.SubmitChanges(); 
} 
else 
dataContext.RejectChanges(); 
//Refresh The List Box Data 
//*** Your Code to Load Re Fetch the Data*** 
}

But wait … this will not work. This is where you need to be aware of async call.

Understanding Asynchronous Operation

"In asynchronous mode, an application will continue with other command or functions while the previously command is still in execution mode"

Now this creates a problem for our above sample. We called dataContext.SubmitChanges(); then in the next line, we are binding the list box item Source. We need to wait for the async call “submitchanges” to be finished. But how do we will handle it ?????…. Simple - using CallBack Functions.

Once the command completes its task, the specified callback function is called where we can write our data load logic.

The Actual Way of Doing It

Instead of using SubmitChanges(), we will use SubmitChanges Method (Generic Action, Object) and we will invoke the data binding logic within the callback function.

C#
void chldWind_Closed(object sender, EventArgs e) 
{ 
ChildWindow chldWind = (ChildWindow)sender; 
if ((chldWind.DialogResult == true)) 
{ 
dataContext.SubmitChanges(OnSubmitCompleted, null); 
} 
else 
dataContext.RejectChanges(); 
} 
/// <summary> 
/// CallBack Function for SubmitChanges 
/// </summary> 
/// <param name="so"></param> 
private void OnSubmitCompleted(SubmitOperation so) 
{ 
if (so.HasError) 
{ 
MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message)); 
so.MarkErrorAsHandled(); 
} 
//Logic to Bind Data to List Box 
LoadData(); 
}

As described above, the SubmitChange line goes like this:

C#
dataContext.SubmitChanges(OnSubmitCompleted, null);

While calling the following “OnSubmitCompleted “ function once, the submit operation is over.

C#
/// <summary> 
/// CallBack Function for SubmitChanges 
/// </summary> 
/// <param name="so"></param> 
private void OnSubmitCompleted(SubmitOperation so) 
{ 
if (so.HasError) 
{ 
MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message)); 
so.MarkErrorAsHandled(); 
} 
//Logic to Bind Data to List Box 
LoadData(); 
}

Conclusion

This is only one pick from the set of such operations. Another example I can mention here is the way the collection is getting loaded using LoadOperation by executing the Entity Query.

So keep Async Operations in mind while coding with SL and WCF RIA service, by doing so not only will you save time, but it also makes her happy :).

Hope my heading does make sense now.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Microsoft
India India
Nothing special .. I like challenges and love my critics.

Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik

Comments and Discussions

 
-- There are no messages in this forum --