deeiip
deeiip

Reputation: 3379

identify callback of which wcf request is current event handler

Say I've this code:

for(i=0;i<5;i++)
{
    client c = new client();
    c.fooAsyncCompleted+=h;
    c.fooAsync(i);
}

Where client class is generated from wcf service reference. Now in function h i need to know of which call of c.fooAsync result is currently being processed? Is there a way do do such thing?

Upvotes: 0

Views: 167

Answers (2)

Gerrit F&#246;lster
Gerrit F&#246;lster

Reputation: 964

If you are using the normal WCF proxy classes generated by Visual Studio you should have a method overload that takes a UserState argument.

c.fooAsync(i, i);

In your completed handler you can retrieve that argument via the EventArgs.

h(object sender, fooAsyncCompletedEventArgs e)
{
    var x = e.UserState;
}  

With that information you will know to which call the callback belongs.

Upvotes: 2

Alex
Alex

Reputation: 23300

You can retrieve the source of the handled event from the first paramter of the handler, object sender

Assuming your handler looks like (generically)

void handler(object sender, EventArgs e)

You can have your client object through simple casting

var source = (client)sender;

How exactly you in turn identify it, is up to you (it depends on the class structure ... Since it needs to be identified, a unique "id" property would do).

Upvotes: 0

Related Questions