WCF/WF - unable to access form controls when WCF service calls back

Asked By praveen kumar on 23-Dec-10 01:31 AM
HI all 

I am Praveen 


am developing one  WCF application using winforms 

Problem

a form has controls like textbox ,button ,listbox  ,in button click event am calling WCF service Method as a responce wcf calls winform client with callback mechanism ,when WCF service calls back am adding list items to list box but form doesnt showing added items in list box  .


thanks in Advance............ 






Indranil Chatterjee replied to praveen kumar on 27-Dec-10 06:43 AM
If you create the service host instance from the UI thread (i.e. after the call to Application.Run), then the service by default joinsthe UI thread. If the service host is created on a different thread (i.e. either before the call to Application.Run or by explicitly creating the host using a different thread), then the service doesn't join the UI thread. In the 2nd case, the service code annot update UI elements directly. You have to update it using Control.Invoke method for the respective UI control you want to update (i.e. for a combo box named myCombo, you need to call myCombo.Invoke). You need to pass a delegate to Invoke method which contains the codeto update the UI (i.e. adding items to the list in this case).