Spawning thread from ThreadPool to perfrom async operation is not recommended and it is explained with detail at
here.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
you normally use async for three cases. the first is to release the thread to the pool to do other work while the async operation is being performed. the second is to run more than 1 async operation at a time. the third is you can do other work while the
async operation is running.
in your case you are making a sync operation async by running it on a new thread, so while you release one thread, you take another. I see no benefit in your approach (unless you have more operations, or a lot of code that can run during the queue). remember
nothing is return to the client until the async operation completes.
I did not write the code, i've been struggling with this for ages, so in the end i freelanced it out and the code is written by another programmer.
What i want the code to do is connect to the database SQL2008R2 via a SP. The SP runs and then returns the results.
In this code the SP is quick, but in another SP which uses FullText Search, some off the results can take about 1 second to return.
Its just that he is using ThreadPool.QueueUserWorkItem and in all the examples i have seen using AsyncController none have used ThreadPool.QueueUserWorkItem
So my question was really is what he has done the correct way to do it.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
as the said the code is correct. QueueUserWorkItem is used when you want to make a sync process async. it does this by running the sync process on a new thread, thus releasing the calling thread free to do other work.
in your case, why are you doing this? are you doing some other code during the 1 minute query (a second query?), otherwise its just using more resources. the asp.net thread is returned to the pool, but another is used to process the query. the request will
not come back any quicker to the user.
note: 1 minute is way too long for a web request. I'd use a background process and ajax polling to check completion, or better yet, make the query run in under 10 secs (the practical max for a web request).
Its just that he is using ThreadPool.QueueUserWorkItem and in all the examples i have seen using AsyncController none have used ThreadPool.QueueUserWorkItem
You should never use QueueUserWorkItem from ASP.NET applications (whether it's WebForms, Web Pages, or MVC).
QueueUserWorkItem schedules work in the thread pool. ASP.NET uses the thread pool to service requests. You're not getting any better parallelism, because the worker thread that's blocked in the thread pool with your synchronous work is no longer available
to answer ASP.NET requests.
Thanks for the reply, looks like i'll have to send him back to the drawing board. Its a bit of a nightmare when i pay a freelancer to do the work and he cannot get it right.
LearningASP_...
Member
242 Points
256 Posts
AsyncManager.OutstandingOperations
Jun 11, 2011 03:07 PM|LINK
public void IndexAsync() { AsyncManager.OutstandingOperations.Increment(); ThreadPool.QueueUserWorkItem(o => { DownloadCurrencyData(); AsyncManager.OutstandingOperations.Decrement(); }, null); } public ActionResult IndexCompleted(IEnumerable<CurrencyData> currencyData) { var portalViewModel = new PortalViewModel { CurrencyFormViewModel = new CurrencyFormViewModel { CurrencyList = new SelectList( currencyData, "CurrencyID", "CountryName") }, WeatherFormViewModel = new WeatherFormViewModel() }; return View(portalViewModel); }imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: AsyncManager.OutstandingOperations
Jun 11, 2011 06:54 PM|LINK
Spawning thread from ThreadPool to perfrom async operation is not recommended and it is explained with detail at here.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: AsyncManager.OutstandingOperations
Jun 11, 2011 10:45 PM|LINK
yes, it looks right. but why?
you normally use async for three cases. the first is to release the thread to the pool to do other work while the async operation is being performed. the second is to run more than 1 async operation at a time. the third is you can do other work while the async operation is running.
in your case you are making a sync operation async by running it on a new thread, so while you release one thread, you take another. I see no benefit in your approach (unless you have more operations, or a lot of code that can run during the queue). remember nothing is return to the client until the async operation completes.
LearningASP_...
Member
242 Points
256 Posts
Re: AsyncManager.OutstandingOperations
Jun 12, 2011 11:26 AM|LINK
Hi
I did not write the code, i've been struggling with this for ages, so in the end i freelanced it out and the code is written by another programmer.
What i want the code to do is connect to the database SQL2008R2 via a SP. The SP runs and then returns the results.
In this code the SP is quick, but in another SP which uses FullText Search, some off the results can take about 1 second to return.
Its just that he is using ThreadPool.QueueUserWorkItem and in all the examples i have seen using AsyncController none have used ThreadPool.QueueUserWorkItem
So my question was really is what he has done the correct way to do it.
Regards
George
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: AsyncManager.OutstandingOperations
Jun 12, 2011 12:01 PM|LINK
I belive you have not read the Rick article still. Please read it.
http://blogs.msdn.com/b/rickandy/archive/2009/11/14/should-my-database-calls-be-asynchronous.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
LearningASP_...
Member
242 Points
256 Posts
Re: AsyncManager.OutstandingOperations
Jun 12, 2011 04:04 PM|LINK
Hi
So much to read, my head is spinning and i'm still none the wiser. Some say yes and some say no
Thanks
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: AsyncManager.OutstandingOperations
Jun 12, 2011 11:43 PM|LINK
as the said the code is correct. QueueUserWorkItem is used when you want to make a sync process async. it does this by running the sync process on a new thread, thus releasing the calling thread free to do other work.
in your case, why are you doing this? are you doing some other code during the 1 minute query (a second query?), otherwise its just using more resources. the asp.net thread is returned to the pool, but another is used to process the query. the request will not come back any quicker to the user.
note: 1 minute is way too long for a web request. I'd use a background process and ajax polling to check completion, or better yet, make the query run in under 10 secs (the practical max for a web request).
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: AsyncManager.OutstandingOperations
Jun 13, 2011 09:10 PM|LINK
QueueUserWorkItem schedules work in the thread pool. ASP.NET uses the thread pool to service requests. You're not getting any better parallelism, because the worker thread that's blocked in the thread pool with your synchronous work is no longer available to answer ASP.NET requests.
LearningASP_...
Member
242 Points
256 Posts
Re: AsyncManager.OutstandingOperations
Jun 13, 2011 09:42 PM|LINK
Hi Bradwils
Thanks for the reply, looks like i'll have to send him back to the drawing board. Its a bit of a nightmare when i pay a freelancer to do the work and he cannot get it right.
Regards
George