线程正在中止 - 循环很多行

时间:2012-11-16 09:15:06

标签: c# asp.net web-config timeout response

我加载了一个有aprox的数据表。 60.000行。

我循环这些,并像这样写它们来构建一个文件:

                HttpContext.Current.Response.Write(lineItem);
                HttpContext.Current.Response.Write(Environment.NewLine);

当我迭代~15-25.000行时,它在5秒内工作,并且文件生成完美。然而,似乎有一个限制,它突然需要很长时间,然后超时(?)。

我收到错误:

  

System.Threading.ThreadAbortException:线程正在中止   在System.AppDomain.GetId()处   System.Threading.Thread.get_CurrentCulture()at   System.String.IndexOf(String value)at   Modules.DownloadReports.RemoveTags(字符串   s)at   Modules.DownloadReports.WriteProductData(数据表   产品)

对于应用程序和SQL调用,我在web.config中的超时时间为3600秒。

我的代码:

private void WriteProductData(DataTable products)
        {
            StringBuilder bld = new StringBuilder();
            try
            {
                //Column names
                const string str = "OrderId; ProductId; ProductName; SeriesName; BrandName; PrimaryCategory; Content; ContentUnit; Quantity; ListPrice; PlacedPrice; Status";
                bld.AppendLine(str);
                bld.AppendLine(Environment.NewLine);

                //Data
                string trackingNumber, productId, productName, seriesName, brandName, prodCategory, content, contentUnit, quantity, listPriceUnit, placedPriceUnit, status;
                string lineItem;
                string errorString = string.Empty;

                foreach (DataRow product in products.Rows)
                {
                  // initialize all the different strings

                  lineItem = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};",
                                                trackingNumber, productId, productName, seriesName, brandName,
                                                prodCategory, content, contentUnit, quantity, listPriceUnit,
                                                placedPriceUnit, status);
                    //bld.AppendLine(lineItem);
                    //bld.AppendLine(Environment.NewLine);
                    HttpContext.Current.Response.Write(lineItem);
                    HttpContext.Current.Response.Write(Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat(ex, "An exception occured during writing of product data to BI report");
                throw;
            }
            finally
            {
                //Response.Write(bld.ToString());
            }
        }

我尝试了什么......

我已经尽可能地优化了循环,所以唯一剩下的就是构建一个字符串,用string.format连接。

我还尝试使用Stringbuilder来构建大字符串,然后将其写出来。

所以我的问题是......

  • 为什么我甚至得到了Threas被中止的例外?在它给我错误之前75秒需要aprox,而不是5分钟
  • ~15-25.000行需要5秒,所以我的逻辑说60.000应该花费最多15秒。这里可能出现什么问题?

更新:

我通过消除长操作的原因解决了这个问题。有一个字符串操作非常慢..当它被称为pr行3次时,它使一切都非常慢。

然而,这不是问题的根源。这只是一个实用的修复,它将起作用,直到数据负载非常大。

1 个答案:

答案 0 :(得分:0)

尝试在网络配置中添加maxRequestLength =“1048576”,这也可能导致您遇到的错误。