线程VS TPL VS异步委托在ASP.NET [英] Threads vs TPL vs Async Delegates in ASP.NET

查看:103
本文介绍了线程VS TPL VS异步委托在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在生产中运行良好的应用程序,但我不知道如果我能已经实现了并发性更好....

ASP.NET .NET 4,C#

基本上,它产生于(目前约为50)飞sql语句的n个,然后运行它们,并同时将数据写入到.csv文件。

编辑:首先,我创建一个线程来执行就这么页面请求可以返回所有的工作。然后在该线程...

对于每一个我创建一个使用TPL和使用DataReader执行它,并将数据写入到磁盘一个新任务的SQL语句。当创建的最后一个文件我总结的一些数据写入到一个汇总文件和zip这一切,并把它交给用户。

我是不是应该使用的线程或异步委托呢?

我没有贴code,因为我真的只是想知道如果我的总体思路(即TPL)是在这种情况下最好的选择。

请不要训斥我有关创建动态SQL,这是完全必要的,因为数据库的技术问题我从读书的问题并不相关。 (它是一种专有系统的后端。拿到7000 +表)。


解决方案

  

我是不是应该使用的线程或异步委托呢?


显然,你的后台线程运行在单个HTTP请求的边界跨越。在这种情况下,它并没有真正不管你用什么API来运行这样的操作: Task.Run Delegate.BeginInvoke ThreadPool.QueueUserWorkItem 新帖或其他任何东西。

您不能同时运行一个漫长的后台线程操作,这一辈子跨越多个HTTP请求,ASP.NET内部地址空间。虽然这是比较容易实现的,这种做法可能与IIS的可维护性,可扩展性和安全性的问题。为创建一个WCF服务和ASP.NET页面调用它:

如何:主机WCF服务中托管Windows服务

I have an application that is working well in production, but I wonder if I could have implemented the concurrency better....

ASP.NET .NET 4, C#

Basically, it generates n number of sql statements on the fly (approx 50 at the moment) and then runs them concurrently and writes the data to .csv files.

EDIT: First I create a thread to do all the work on so the page request can return. Then on that thread...

For each of the SQL statements I create a new Task using the TPL and execute it using a datareader and write the data to disk. When the last file is created I write some summary data to a summary file and zip it all up and give it to the user.

Should I have used Threads or Asynchronous Delegates instead?

I haven't posted code as I am really just wondering if my overall approach (i.e. TPL) is the best option in this situation.

Please don't lecture me about creating dynamic sql, it is totally necessary due to the technicalities of the database I am reading from and not relevant to the question. (Its the back end of a proprietary system. Got 7 thousand+ tables).

解决方案

Should I have used Threads or Asynchronous Delegates instead?

Apparently, your background thread operation spans across the boundaries of a single HTTP request. In this case, it doesn't really matter what API you use to run such operation: Task.Run, Delegate.BeginInvoke, ThreadPool.QueueUserWorkItem, new Thread or anything else.

You shouldn't be running a lengthy background thread operation, which lifetime spans multiple HTTP requests, inside ASP.NET address space. While it's relatively easy to implement, this approach may have issues with IIS maintainability, scalability and security. Create a WCF service for that and call it from your ASP.NET page:

How to: Host a WCF Service in a Managed Windows Service.

这篇关于线程VS TPL VS异步委托在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆