When does the loadoperation completed event fire?
Class DomainContext
has开发者_JAVA百科 method Invoke which return instance of InvokeOperation
and often we can see next code
InvokeOperation op = domainConextInstance.Invoke(...);
op.Completed +={...};
My first thought - it should not work: after all event can arise earlier than we will subscribe for it.
I made an experiment
InvokeOperation op = domainConextInstance.Invoke(...);
Thread.Sleep(5000); //or 25000
op.Completed +={...};
But I found that this code works correctly, But how? Can you explain this to me?
And what pattern does this construct use?
It's hard to know without seeing any of the code for DomainContext
- but it sounds like the code which adds a handler for the Completed
event calls the handler immediately if the operation has already completed.
Assuming you have the code for InvokeOperation
, I'd definitely look at the declaration of the Completed
event to discover the "magic".
Assuming, you are talking about WCF RIA Services SDK, Jon is right. InvokeOperation has a property IsComplete
. The add
part of the Completed event checks this property. In case of a completed operation it does not add the passed event handler but calls it immediately.
You can validate this by inspecting OperationBase (base class of InvokeOperation) in System.ServiceModel.DomainServices.Client.dll with a disassemler tool like dotPeek.
精彩评论