开发者

Can the following code be editted, so that the second method is executed too?

开发者 https://www.devze.com 2023-02-22 05:55 出处:网络
Is there anyway of calling methodB before methodA finishes, so that methodB gets executed? Ive tried numerous ways of implementing it, and none of them worked :(

Is there anyway of calling methodB before methodA finishes, so that methodB gets executed? Ive tried numerous ways of implementing it, and none of them worked :(


can someone explain? Thanks.

public List<ObjectA> _OurObjectA= new List<ObjectA>();

public  List<ObjectA> methodA()
{

    List<ObjectA> products = new List<ObjectA>();
    ServiceReference1.ServiceClient client = 
      new ServiceReference1.ServiceClient();
    clie开发者_StackOverflownt.getProductCompleted += 
      new EventHandler<ServiceReference1.getProductCompletedEventArgs>(methodB);

    client.getProductAsync();

    return _OurObjectA;
}

void methodB(object sender, ServiceReference1.getProductCompletedEventArgs e)
{
    ObservableCollection<ServiceReference1.ObjectA> products = e.Result;
    foreach (ServiceReference1.ObjectA obj in products)
    {
        ObjectA temp = new ObjectA(obj);
        _OurObjectA.Add(temp);
    }
}


It looks like methodA calls a web service. When the web service is done it calls methodB. The confusing part is that this is asynchronous (via the event handler)... methodA returns right away; it doesn't have to wait for the web service to finish.

This was probably done because the service takes a long time (relatively speaking), so in this case you can return from methodA right away, do some other work, and when the web service is done methodB can do it's stuff with the data returned from the web service. This is a more efficient use of resources. Sometimes this sort of thing is done so that the UI doesn't look like it's "hanging."


Your client object has an event called getProductCompleted. When that event is published, methodB is executed. client.getProductAsync() is an asynchronous call, meaning it will start downloading a product in the background even though the method will return immediately. When the product is finished downloading, the client publishes the event, causing methodB to be executed.


It assigns the method methodB as an event handler for the event getProductCompleted. When the event is raised by the client variable, the event handler is executed.

UPDATE:
I update my answer to actually answer your question...
You can use this code, to return from methodA after the products have been returned:

public  List<ObjectA> methodA()
{
    var products = new List<ObjectA>();
    var mre = new ManualResetEvent(false);
    var client = new ServiceReference1.ServiceClient();
    client.getProductCompleted += (s, e) => {
                                               try
                                               {
                                                   products.AddRange(
                                                       e.Result.Select(o => 
                                                            new ObjectA(o)));
                                               }
                                               finally
                                               {
                                                   mre.Set();
                                               }
                                            };

    client.getProductAsync();
    mre.WaitOne();

    return products;
}

You don't even need methodB in that case.
The code assumes, you are using at least .NET 3.5.


And there's apparently an error in methodB:

 foreach (ServiceReference1.ObjectA obj in products)
    {
        ObjectAtemp = new ObjectA(obj);
         // no temp -- there should be a space between ObjectA and temp in the line above
         // perhaps just a typo here on SO? or in the original code?
         // EDIT: I see the code in methodB has now been corrected.
         _OurObjectA.Add(temp);
    }
0

精彩评论

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

关注公众号