You can "emulate" synchronous call using ManualResetEvent.Here is code from my project (modified it a bit to remove project-specific code). It works for spatial query but you can always modify it to use "where" clause.
public FeatureSet Query(Geometry geometry, string layerURL)
{
if (Dispatcher.CheckAccess())
{
var message = string.Format("\"Query\" method should be called on background thread.");
throw new Exception(message);
}
FeatureSet result = null;
Exception error = null;
var manualResetEvent = new ManualResetEvent(false);
Dispatcher.Invoke(() =>
{
var queryTask = new QueryTask(layerURL);
var query = new Query();
query.OutFields.Add("*");
query.Geometry = geometry;
query.ReturnGeometry = true;
query.OutSpatialReference = Map.SpatialReference;
EventHandler<TaskFailedEventArgs> onTaskFailed = null;
EventHandler<QueryEventArgs> onExecuteCompleted = null;
onTaskFailed = (s, a) =>
{
queryTask.ExecuteCompleted -= onExecuteCompleted;
queryTask.Failed -= onTaskFailed;
error = a.Error;
manualResetEvent.Set();
};
onExecuteCompleted = (s, a) =>
{
queryTask.ExecuteCompleted -= onExecuteCompleted;
queryTask.Failed -= onTaskFailed;
result = a.FeatureSet;
manualResetEvent.Set();
};
queryTask.ExecuteCompleted += onExecuteCompleted;
queryTask.Failed += onTaskFailed;
queryTask.ExecuteAsync(query);
});
manualResetEvent.WaitOne();
if (error == null)
{
return result;
}
else
{
throw error;
}
}You can get Dispatcher instance from any UI control, for example layout root.Also you must call this method on background thread (exception on the first line is for a purpose) because manual reset event will block UI thread and entire application. You can do this by using BackgroundWorker:
backgroundWorker.DoWork += (s, a) =>
{
[your code here]
var featureSet = Query(geometry, layerURL);
[your code here using featureSet]
var maybeAnotherFeatureSet = Query(geometry2, layerURL2);
[you code here using both feature sets]
};
backgroundWorker.RunWorkerCompleted += (s, a) =>
{
[output results and throw errors here]
};
backgroundWorker.RunWorkerAsync();Using this approach you can group and chain asynchronous calls into one asynchronous background worker call.P.S. so much code/text here, now I think it can be overkill for your problem.