Search code examples
c#silverlightwcf-ria-services

Delaying between two consecutive calls


Using Silverlight 5, RIA services 1.0 and Telerik controls I have a dialog with a button that when clicked goes and runs some service code. The issue is when I double click or triple click it real fast, it keeps calling the service thus getting this error:

System.InvalidOperationException: 
System.InvalidOperationException: A SubmitChanges operation is already in progress on this DomainContext.

I was wondering if this is a common error and any work around for it?

Here is the .NET source code that it goes to that causes it to crash:

 public virtual SubmitOperation SubmitChanges(Action<SubmitOperation> callback, object userState)
    {
      if (this.IsSubmitting)
        throw new InvalidOperationException(Resource.DomainContext_SubmitAlreadyInProgress);

Solution

  • This is not an error. A submitChange operation is asynchronous so you have to detect that it is completed before doing something else.

    One solution could be to block the user from clicking on the button before the operation is completed.

    Since you are using a Telerik controls, you can use a busy indicator.

    private void btnUserAction_Click(object sender, RoutedEventArgs e)
    {
        myBusyIndicator.IsBusy = true;
    
        // DO your stuff
    
        var submitOperation = myContext.SubmitChanges();
    
        submitOperation.Completed += (s, e) =>
            {
              // It is completed, now the user can click on the button again 
    
              myBusyIndicator.IsBusy = false;
             }
    }
    

    EDIT : The busy indicator should be defined in your Xaml, like this :

    <Telerik:RadBusyIndicator x:Name="myBusyIndicator">
                <Grid x:Name="LayoutRoot" >
                     <Button Name="btnUserAction" Click="btnUserAction_Click" />
                </Grid>
    </Telerik:RadBusyIndicator>