There's no way for the server side ASP.NET code to poke the web browser and to tell it that there's new data available. So you'll have to use to JavaScript to poll the server for new data. There are various ways of doing this. One example would be to use Page Methods on the server side that could tell the client whether there's more data available and when all the data has been loaded.
[WebMethod]
public static bool IsNewDataAvailable(int currentClientRows)
{
return dataCollectedSoFar.Count > currentClientRows;
}
[WebMethod]
public static bool IsFinished()
{
// return true if all the threads in the thread pool are finished
}
You need to call the IsNewDataAvailable method at regular intervals. A simple JavaScript timer should do the trick.
When there is new data available, you'll need to re-render the GridView. Again, there's more than one way to do this, but a nice and simple way would be to put the GridView inside an UpdatePanel along side a Button with a style="display: none;" attribute to keep it hidden. Then if there is new data available, simply call the JavaScript click method on the button to update the contents of the update panel.
<script>
var timerId = setInterval("checkForData()", 5000);
function checkForData() {
// If all threads have finished, stop polling
if (PageMethods.IsFinished()) {
clearInterval(timerId);
}
var currentRowCount = 0;
// Find out how many rows you currently have, if
// you have jQuery you could do something like this
currentRowCount = $("#<%= myGridView.ClientID %> tr").length;
if (PageMethods.IsNewDataAvailable(currentRowCount)) {
// Here we trigger the hidden button's click method, again
// using a bit of jQuery to show how it might be done
$("#<%= myHiddenButton.ClientID %>").click();
}
}
</script>
. . .
<asp:UpdatePanel ID="myUpdatePanel" runat="server">
<ContentTemplate>
<asp:GridView ID="myGridView" runat="server">
. . .
</asp:GridView>
<asp:Button ID="myHiddenButton" runat="server" style="display: none;" OnClientClick="myHiddenButton_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Finally, to populate the GridView on the server side, you can continue to use a ThreadPool and just render all the data you have each time. For example:
protected void myHiddenButton_Click(object sender, EventArgs e)
{
myGridView.DataSource = dataCollectedSoFar;
myGridView.DataBind();
}