开发者

is there a way of implementing the following method, such that is gets called in time?

I now have the following code, which calls a WCF service, it g开发者_运维技巧et some data and then pass the data on to the browser.

The only problem is that client_GetProductCompleted() does not get called until Load() finishes, and by then, it is too late. The browser has already loaded the information.

So basically I am asking, if there is a way of implementing the methods, such that client_GetProductCompleted() gets called as soon as Load() gets called, then it can return the data back to the browser in time :)

It is probably silly, but I have tried to ask the program to sleep while waiting for Load() to finish, and it didnt work. So Im totally clueless now ...

public static Products _OurProducts = new Products();

public void Load()
{
    ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
    client.getProductsCompleted += 
        new EventHandler<ServiceReference1.getProductsCompletedEventArgs>(
            client_GetProductCompleted);

    client.getProductsAsync();    
    return;
}

void client_GetProductCompleted(object sender, ServiceReference1.getProductsCompletedEventArgs e)
{
    if (e.Result != null)
    {
        ObservableCollection<ServiceReference1.Product> products = e.Result;
        foreach (ServiceReference1.Product prod in products)
        {
            Product temp = new Product(prod);
            _OurProducts.Add(temp);
        }
     }
}


It isn't completely clear, but I suspect you want MethodA to block until the handler finishes executing (presumably on another thread).

It's possible that the object you are querying offers a synchronous way of returning its results. This would definitely be the way to go if available.

If not, you can make the method block until it is signaled, such as with a ManualResetEvent.

E.g.:

var resetEvent = new ManualResetEvent(false);
client.getProductsCompleted += (s, e) => 
                               { 
                                   client_GetProductCompleted(s, e); 
                                   resetEvent.Set();
                               };    
client.getProductsAsync(); 
resetEvent.WaitOne();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜