public class AllViewModel
{
private List<Settings> SettingsList;
public ViewAgendaAllViewModel()
{
client.SupplierListWithSettings(GetSupplierListWithSettings_Completed)
}
public void GetSupplierListWithSettings_Completed(object sender, Supplier_GetListWithSettingsCompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result != null)
{
SettingsList = new List<Settings>();
foreach (VCareSupplierDto obj in e.Result)
{
SettingsList.Add(obj);
}
}
}
}
}
Issue: Settinglist propery not getting set because of asynch call.
this is my class i want to synchronus call to ServiceMethod SupplierListWithSettings.
When I will create instance of the AllViewModel it should loaded with setting.
Expected : When i will create instance of AllViewModel it should contain SettingList property.
Correction: SettingsList is getting set, but it sounds like it’s not set as soon as you would like.
It sounds like you’re asking for this: when you construct an instance of AllViewModel, you expect the SettingsList property to be fully populated before the AllViewModel constructor returns.
While this can be done, there are many reasons why this is probably not a good idea and a dubious design requirement. Making the network call into a synchronous call will block the calling thread (usually, your UI thread) for as long as it takes for the network request to complete, which could be 20 to 30 seconds. Do you really want your application UI to freeze for 30 seconds? Your users are likely to assume your program has crashed or “locked up” and will shut down the program without even realizing that it isn’t dead.
Nevertheless, here’s how to do it: You can convert the asynchronous call into a synchronous call using something like this (untested) using the .NET 4.0 Task Parallel Library (TPL):
Again, this is not a good idea. It would be a much better exercise for you to look at what portions of your code are assuming the SettingsList property is populated immediately after the object is constructed and change those dependencies to do something like listen for changes to the SettingsList property and do their work as soon as SettingsList is actually populated asynchronously.
Try to go with the asynchronous flow instead of fighting against it.