3

I've a heavy operation which takes long time to be completed in my asp.net application. I don't want to run the entire operation in one request which may result in a TimeOut error. Instead I want to invoke the operation in a separate thread and poll for the result every x seconds. How can I do this?

If the operation gets completed I need to register a script in the ajax postback to hide the loading panel and show the content. However I'm not able to register new scripts and invoke it during ajax postbacks.

Any ideas?

NLV
  • 21,141
  • 40
  • 118
  • 183

2 Answers2

4

Option 1

In your code-behind you can launch a thread and have some class that manages the running threads. Then you can have a control in an UpdatePanel which polls for the progress every few seconds.

Option 2

Store the details of the "thing to do" in the database and have a background service continually check for work to do. It would do the work and report progress. You would have a control like in Option 1 which checks the progress and updates the UI.

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • I've multiple web parts in my page for each I would like to create a thread (I want them to be loaded independently). In that case even if 1000 users connect at the same time and if I've 10 web parts in a page wont it create 10000 threads? Is it perfectly normal to have these many threads? – NLV Mar 31 '11 at 04:10
  • 1
    Two things. If you know all the work that needs to be done ahead of time, then use Parallel.ForEach() to process the items in parallel. This is the most efficient way to make use of a system with multiple processors or cores. If you want to use a Thread, it is important to use ThreadPool.QueueWorkitem() to queue your item to be worked (instead of new Thread()). The ThreadPool will manage and reuse threads which is much better performance-wise than creating your own, fresh threads. And if you queue up 1,000 items, the ThreadPool will work them as threads become available. I hope that helps you! – Josh M. Mar 31 '11 at 11:31
  • Considering any option that uses thread, what `Session` object will it have access to? – Nikhil Girraj Jun 22 '16 at 12:28
  • @NikhilGirraj You should pass any info you need from the session to the function which is executing on another thread. – Josh M. Jun 22 '16 at 17:08
1

On postback I will create a Thread that launch the long running process and also register client startup script. The client script will use setTimeOut that calls your predefine js function to launch the first webservice call to pool for status update. If result shows that it has finished, you can change the panel visibility in the javascript. Otherwise, it will schedule to invoke your js function again after x millisecond through setTimeOut call again.

Fadrian Sudaman
  • 6,405
  • 21
  • 29
  • As I said while doing partial postbacks I'm not able to register a new script and invoke it. – NLV Mar 24 '11 at 12:01
  • ClientScriptManager registered script only run on initial rendering but you should be able to do that using ScriptManager.RegisterStartupScript for your partial rendering through postback. – Fadrian Sudaman Mar 24 '11 at 12:04