开发者

Calling a async method multiple times

I am calling a async method having a single parameter, It will return me the result according to parameter. I am calling that method more than one time with different parameter value, but in Completed event i am getting the same value f开发者_开发百科or all.

client.ListAllLookupValuesByTypeCompleted += client_ListAllAddressFormatCompleted;
client.ListAllLookupValuesByTypeAsync("AddressFormat");

client.ListAllLookupValuesByTypeCompleted += client_ListAllPhoneFormatCompleted;
client.ListAllLookupValuesByTypeAsync("PhoneFormat");



void client_ListAllAddressFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e)
        {
            cmbAddressFormat.ItemsSource = e.Result;
        }


void client_ListAllPhoneFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e)
        {
            cmbPhonePrintFormat.ItemsSource = e.Result;
        }

any suggetions. Thanks.


Got the Answer Your method may return a different value based on the first parameter, but both handlers will be called at the same time every time, regardless of what you send it. If this is a standard webservice reference, then you should see an object userState parameter available for you and this can be used to determine what to do.

client.ListAllLookupValuesByTypeCompleted += client_ListAllLookupValuesCompleted;
client.ListAllLookupValuesByTypeAsync("AddressFormat", true);
client.ListAllLookupValuesByTypeAsync("PhoneFormat", false);



void client_ListAllLookupValuesCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e)
        {
            // e.UserState will either be false or true
            if ((bool)e.UserState)
               cmbAddressFormat.ItemsSource = e.Result;
            else
               cmbPhonePrintFormat.ItemsSource = e.Result;
        }


You should either only add the completed event handler once and pass back the type of data retrieved in the ListAllLookupValuesByTypeCompletedEventArgs object:

client.ListAllLookupValuesByTypeCompleted += client_ListFormatCompleted;
client.ListAllLookupValuesByTypeAsync("AddressFormat");
client.ListAllLookupValuesByTypeAsync("PhoneFormat");

void client_ListFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e)
{
    if (e.Type == ResultType.AddressFormat)
    {
        cmbAddressFormat.ItemsSource = e.Result;
    }
    else
    {
        cmbPhonePrintFormat.ItemsSource = e.Result;
    }
}

or have two separate events one for each data type:

client.ListAddressLookupValuesCompleted += client_ListAddressFormatCompleted;
client.ListAddressLookupValuesAsync();

client.ListPhoneLookupValuesCompleted += client_ListPhoneFormatCompleted;
client.ListPhoneLookupValuesByTypeAsync();

void client_ListAddressFormatCompleted(object sender, ListAddressValuesCompletedEventArgs e)
{
    cmbAddressFormat.ItemsSource = e.Result;
}

void client_ListPhoneFormatCompleted(object sender, ListPhoneValuesCompletedEventArgs e)
{
    cmbPhonePrintFormat.ItemsSource = e.Result;
}

In this case you'll need to refactor your server side code to match.


without source code it is a little hard to guess the problem but I think the problem is, that you add the same completed handler several times. Like this:

for(int i =0; i < 10; i++)
{
  ws.callCompleted += CallCompletedHandler;
  ws.callAsync(i);
}

void CallCompletedHandler(object sender, EventArgs args) 
{
  handle result
}

Do you remove the handler in the completed event?

 void CallCompletedHandler(object sender, EventArgs args) 
    {
     ws.callCompleted -= CallCompletedHandler;
      handle result
    }

This could solve your problem.

Here are some other ideas: As you call is async it can be that you completed handler is called up to ten times for each call (cause you added it 10 times). You can use the UserState Parameter (http://msdn.microsoft.com/en-us/library/wewwczdw(v=vs.80).aspx) so you can match your completed handler with your call.

for(int i =0; i < 10; i++)
    {
      ws.callCompleted += CallCompletedHandler;
      ws.callAsync(i, i); //Second param is user state
    }

    void CallCompletedHandler(object sender, EventArgs args) 
    {
      if(args.UserState == //Your check here;)
{
      ws.callCompleted -= CallCompletedHandler; //Remove the handler
      handle result
}
    }

The problem here is, that you need some sort of class variable to track your UserStates. If you write it like this, you don't have to do this.

for(int i =0; i < 10; i++)
        {
          CallWebservice(i);

        }

        void CallWebservice(int i)
        {
          EventHandler myHandler= null;
          myHandler = (s, args) => {
          if(args.UserState == i){
            ws.callCompleted -= myHandler; //Remiov
            Handleresult
          };
          ws.callCompleted += myHandler; //Add the handler
          ws.callAsync(i, i); //Call the ws
        }
     }

If you have any further questions, jsut leave a comment. If you could provide some source code, I think we can help you more.

BR,

TJ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜