简体   繁体   中英

How do I get the return value of DispatcherOperation inside the callback

I am subscribing to DispatcherOperation's Completed event when the delegate finishes processing. Can any one please tell me how to get the value returned by the delegate inside the Completed event handler. I think it is only possible to not to block the main thread by accepting the return value inside the Completed event handler.

DispatcherOperation dispOp  = this.Dispatcher.BeginInvoke(balUpdater,
   GlobalParams._sessionObject.UserInfo.CardData);
dispOp.Completed += new EventHandler(dispOp_Completed);

void dispOp_Completed(object sender, EventArgs e)
{
   // accept return value of balUpdater here.
}

I would guess the sender parameter is quite possibly the DispatcherOperation , but if not, you can just capture the DispatcherOperation in a delegate:

DispatcherOperation dispOp  = this.Dispatcher.BeginInvoke(balUpdater,
   GlobalParams._sessionObject.UserInfo.CardData);
dispOp.Completed += (sender, args) => HandleCompletion(dispOp);

...


private void HandleCompletion(DispatcherOperation operation)
{
    object result = operation.Result;
    // Use the result here
}

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