none
Stop main thread until WCF Call returns?

    Question

  • I am new to web services and started out with WCF. I have multiple web service calls,each of which are done asynchronouslly. But my problem is that the main thread should stop untill all the web service calls return abd when all the web service calls return only then it should proceed.Below is my sample code :

    ServerMonitoringBoardDataService.ServerMonitoringBoardDataServiceClient c = new ServerMonitoringBoardDataService.ServerMonitoringBoardDataServiceClient();
    c.GetEnvironmentAndServersCompleted += new EventHandler<ServerMonitoringBoardDataService.GetEnvironmentAndServersCompletedEventArgs>(c_GetEnvironmentAndServersCompleted);
    c.GetEnvironmentAndServersAsync();
    
    void c_GetEnvironmentAndServersCompleted(object sender, ServerMonitoringBoardDataService.GetEnvironmentAndServersCompletedEventArgs e)
    {
        var x = e.Result;
    }



    The reason I am facing problems is that,the multiple web service calls returns data as lists and I have done some operations on this data and then displayed it on the UI.The web service calls are made in a static constructor,so as to fetch the data only once and manipulate and display it many time.

    But what happens is that the main thread does not stop until the data is fetched and moves onto perform the operations,where I get a Null Exception.

    Can someone please look into this.




    Monday, May 14, 2012 1:13 AM

Answers

  • Hi,

      Well, you can try this . 

      Take a boolean variable  static bool bRepeat =true ;

      Just after c.GetEnvironmentAndServersAsync(); , wrtie the following loop.

    while(bRepeat) {}   // This pauses the main thread execution till the service completes.

    In the service completed event handler, change bRepeat to false; This ends the loop.

    void c_GetEnvironmentAndServersCompleted(object sender, ServerMonitoringBoardDataService.GetEnvironmentAndServersCompletedEventArgs e)
    {
        var x = e.Result; bRepeat=false;
    }

    Hope it Helps Laughing

     

    Monday, May 14, 2012 1:54 AM

All replies