views:

50

answers:

1

Sorry I cannot post the code but here is what is happening in detail.

User opens a form it adds an event listener like:

GP.GlobalInfo.CommWCF.serviceClient.GetFilteredMessageCompleted += new EventHandler<GetFilteredMessageCompletedEventArgs>(serviceClient_GetFilteredMessageCompleted);

and then sends a asynchronous message to the server to get required data from database. at the same time, the form adds another event listener and then sends another message:

GP.GlobalInfo.CommWCF.serviceClient.GetFilteredMessageCompleted += new EventHandler<GetFilteredMessageCompletedEventArgs>(serviceClient_GetFilteredMessageCompletedAnother);

so the client (form) is waiting for two messages to be returned. as soon as messages are returned the form will display the data in a grid.

It seems not working as I expect. somehow messages get stuck somewhere. so, I would like to know if my approach is wrong.


Another case is, there are five objects on the screen that I can click. These five objects will use the same Class to create. clicking an object brings a form with corresponding data. if I click the first object, the form will add an event listener and sends a message to the server. while the form is waiting for the data, I close the form, and then open another form by clicking another object. The new form will add an event listener and sends a new message to the server.

Meantime, the server has been working the first form message, and returns the result to the second form.. not the first one(this has been closed).

this is what I experience right now. can anybody give me some solution ASAP for the better behavior and understanding of silverlight and WCF. I think I should send a stop message to the server or something..

-------old question----------

Hi I seem to have some problem with communicating between client and server .

when a form is opened, there are three listeners are opened. Each listener gets the required data.

I think (not sure) silverlight gets stuck while it communicates using those three listeners at the same time.

So, should I use only one listener running at the same time always in silverlight? so like,

client asking A data from server
client asking B data from server client save C data to server

those stuffs cannot be done at the same time ? Do I always make sure of single communication process between client and server?

Thanks

A: 

The problem is that the event handler just tells "when that kind of operation is completed, call me". The event handler is not related to the specific call, but to the kind of operation.

If you are calling the same operation twice you cannot attach two event handlers and expect that each call invokes the respective handler. What happens is that when an operation is completed every handler attached to that operation will be invoked.

You can solve your problem as follows:

  • attach a single event handler for each specific operation
  • when you call yourOperation_Async() you can pass an identifier as userState (the last parameter)
  • inside the event handler you act accordingly to the identifer (you can retrieve it from the yourOperationCompledEventArgs.UserState), for ex. updating the correct form.

dummy example:

...
serviceClient.YourOperationAsync(formName);
...

void serviceClient_YourOperationCompleted(object sender, YourOperationCompletedEventArgs e)
{
    if (e.UserState != null && e.UserState is string)
    {
       string formToUpdate = (string)e.UserState;
       ...
       ... update formToUpdate ...
       ...
    }
}
Francesco De Vittori