OK, here is the problem: the Async attribute is designed for the case when your page will cause some long-term task that also blocks the flow, and then your page needs to exit this task in order to return the information to the User. For example, if your page is needed to call a web service, wait for it to respond, and then use the data from the response to display your page.
The reason for using the Async attribute is to prevent thread blocking. This is important because ASP.NET applications use a thread pool to serve requests, and only a relatively small number of threads are available. And if each call associates a thread while waiting for a web service call, then soon you will encounter a sufficient number of concurrent users who will have to wait until the calls to these web services are completed. The Async attribute allows a thread to return to the thread pool and serve other concurrent visitors on your website, rather than making it sit and do nothing while waiting for the web service call to return.
The result is for you: the Async attribute is designed for the case when you cannot display the page until the asynchronous task is completed and why it does not display the page immediately.
You need to start your own thread and make it a daemon thread. I donβt remember the exact syntax for this, but you can easily find it in a document by searching for a BCL document for daemon. This means that the thread will close your application while it is alive, which is important because ASP.NET and IIS reserve the right to βre-process your processβ when they consider it necessary, and if this happens while your thread is running, Your task will be stopped. Creating a thread daemon will prevent this (with the exception of some possible cases of rare edges ... you'll learn more when you find documentation on this).
This daemon flow is where you begin to perform these tasks. And after you told the daemon thread to complete the task, you can immediately display your page ... so that the page will render immediately.
However, even better than the daemon thread in your ASP.NET process, it will implement a Windows service to accomplish this task. Ask the ASP.NET application to submit the task that will run in the Service. There is no need for a daemon thread, and you do not have to worry about your ASP.NET process being processed. How do you inform the Service about this task? Perhaps through WCF, or, perhaps, by inserting a record into the database table that the Service polls. Or a few other ways.
EDIT: Here is another idea that I used earlier for the same purpose. Write information about your task to the MSMQ queue. Ask another process (perhaps even on a different machine) to pull it out of this queue and perform a time-consuming task. The task of inserting into a queue is optimized to return as quickly as possible, so your stream will not be blocked, and the data that you put in the queue is sent by wiring or something like that. This is one of the fastest ways to pay attention to the fact that you need to complete a task without waiting for the task to complete.
Charlie Flowers Mar 23 '09 at 5:22 2009-03-23 ββ05:22
source share