简体   繁体   中英

When does the loadoperation completed event fire?

Class DomainContext has 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM