First, here is a simplified JavaScript API example to show the concept of using DeferredList to process multiple identify tasks:
//Assume that map is your map object
var idTask1, idTask2, idParams = new esri.tasks.IdentifyParameters();
var url1 = "<server1 url>", var url2 = "<server2 url>";
dojo.connect(map, "onLoad", initIdentifies);
function initIdentifies(map) { //map.onLoad passes in the map object
idTask1 = new esri.tasks.IdentifyTask(url1);
idTask2 = new esri.tasks.IdentifyTask(url2);
//A few sample constant parameters. Set more or less as you need
idParams.tolerance = 12;
idParams.returnGeometry = true;
idParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
dojo.connect(map, "onClick", runIdentifies);
}
function runIdentifies(evt) {
var defTask1 = new dojo.Deferred(), defTask2 = new dojo.Deferred;
var dlTasks = new dojo.DeferredList([defTask1, defTask2]);
dlTasks.then(showResults); //defTasks will fire after defTask1 and defTask2 have completed
//These parameters change with each request
idParams.width = map.width;
idParams.height = map.height;
idParams.geometry = evt.mapPoint;
idParams.mapExtent = map.extent;
try {
idTask1.execute(idParams, defTask1.callback, defTask1.errback); //Pass the response into the callback on defTask1
} catch (e) {
console.log("Error caught");
console.log(e);
defTask1.errback(e); //If you get an error, execute the errback
}
try {
idTask2.execute(idParams, defTask2.callback, defTask2.errback); //Pass the response into the callback on defTask2
} catch (e) {
console.log("Error caught");
console.log(e);
defTask2.errback(e); //If you get an error, execute the errback
}
}
function showResults(r) {
//The format of 'r' is [[Boolean task 1 success, [task 1 results]],[Boolean task 2 success, [task 2 results]]]
//using the array 'r', build and show your infoWindow as normal
}
Then here is an example in jsFiddle that I think does what you want, runs using all visible layers in all visible dynamic map layers in the map.
http://jsfiddle.net/blordcastillo/mULcz/
All typos are fixed now :)
The basic idea is that whenever the map is clicked or visibility is toggled, the identify is rerun. When the identify is run, the number of identity tasks fired depends on the number of layers visible, and it waits until all layers return to display its results.