Reputation: 693
I have an asp.net page that will need to do some asynchronous work, e.g visiting URLs and downloading some content which may take several minutes. During this time, as each task completes it needs to update something on the page to indicate progress etc. What's the preferred approach for this? calling some client side javascript and passing the data to it?
I'm trying to use the .NET Task Parallel Library stuff for the asynchronous actions.
Thanks for your time
Upvotes: 4
Views: 1085
Reputation: 8085
Because of the way the internet works you would need to have client side script poll the server. The server is always listening for a request from the client but clients aren't always listening for a response from the server so unless the client initiated the request it won't be waiting for a response.
As for how to do it, I have used JQuery to hit an ASP.Net web service in the past and been very pleased. If you go this route ASP.net will automatically convert between JSON to .Net objects and back again which is very handy.
Here is a post showing you how to use JQuery to hit a web service, and another one on how to set up the web service on the server.
Also, you typically don't want a web service that takes several minutes to complete since this can cause you to run out of threads on the server if the page is hit often. You may want to see about setting up a scheduled task to pre-calculate this data or adding indexes to your data source so you can look it up more quickly. Either way as long as you are using AJAX your user's web-interaction won't be locked down (i.e. they will still be able to click on links and interact with your site. If they navigate away from the page though while it is loading the browser will close the connection and the user will to start a new request and wait the whole time all over again to get the response.)
Unfortunately I don't know anything about the new .NET Task Parallel Library but perhaps one option you could pursue, depending on what you are querying, would be to make multiple AJAX requests for different chunks of the data. That would automatically make things parallel on the server side while giving you the added benefit of showing progress on the client side as each of those requests came back independently. If that doesn't work another popular pattern to notify your user of progress on long running requests is to pass back data every so often along with a "continuation token." As long as the client-side code receives a continuation token it knows there is more data and it passes that token back to the server with the next request. The server then uses this token to pick up where it left off last time. So it could be something as simple as which RowID it stopped on last time.
Upvotes: 4
Reputation: 33379
You might want to check out the SignalR library which allows for real-time communication/events with the server over HTTP, via long polling, or even Web Sockets for newer browsers.
Upvotes: 3