John Lord
John Lord

Reputation: 2185

C# Best way to keep UI responsive during heavy workload

Our webserver generates a file on the fly for download. This takes about two minutes of heavy processing because it's taking 1000 word templates, mail merging them, converting them to pdf, then making a zip out of those 1000 files. The processing is freezing the web server from being able to do anything else in the mean time which is a problem since it's hosting 23 subdomains for clients and they noticed it freezing.

How can i force the UI thread to get some work done? I've been looking at Thread.Sleep and Thread.Yield but perhaps i'm not implementing them correctly. I'm very new to the concept of threading.

Upvotes: 0

Views: 231

Answers (1)

user47589
user47589

Reputation:

When starting the processing on the web server, generate a "job ID" and store it somewhere (such as a database). Add an endpoint so the client can query the status of the job. When the processing is complete, the user can use the job ID to get the resulting file(s). It works like this:

  • User wants to process files. They call the start endpoint, which returns a JobId.
  • The server begins processing that job in a non-request thread, or the job is picked up and processed by another server dedicated to that task. When the thread completes, it updates the job's status accordingly.

Later...

  • User wants to know the status of their process. They call the status endpoint using their JobId periodically. The server replies with some status information to show the user.

Later...

  • Once the job's status has changed to 'complete', the user can call the result endpoint with their JobId to get the final file(s).

The heavy processing should be done in a non-UI, non-request thread so other users are unaffected.

Using this approach, you can even do the processing on another server entirely. All the web server is doing is allowing the user to create and query processing jobs.

Upvotes: 1

Related Questions