The buffer size of streamreader is insufficient. As a result, only part of the uploaded file is processed.

Source: Internet
Author: User

Extract originalCodePart

Streamreader sr = New  System. Io. streamreader (stream, encoding );  //  Sr. discardbuffereddata ();                      Int Totalcount = 0  ;  Using  (SR ){  Int Line = 0  ; While (Sr. Peek ()> 0  ){ ++ Line;  String Temp = Sr. Readline ();

Stream is the uploaded file stream.

The file is in CSV format and I processed it in batches. A total of 20 thousand lines of files are processed each time. The file size is 716kb.

Debugging in vs is normal, but after being published to IIS, it crashes after processing lines each time. Later, Every time 200 rows, 500 rows, and 1500 rows were processed, they all crashed near 2000 rows.

Preliminarily judge similar problems of stack overflow.

Later, I checked the system log and reported that the "file to be read has been closed" in the Readline method ".

Later changed:

Streamreader sr = new system. Io. streamreader (stream, encoding, true, 256*1024 );

It is found that about rows are stopped. The problem lies here.

Write as follows:

Streamreader sr = new system. Io. streamreader (stream, encoding, true, convert. toint32 (stream. Length ));

You can read the file.

The disadvantage is that the file length cannot exceed int32.maxvalue.

 

I also found a problem that previously plagued me:

The difference between a background thread and a common thread is that an exception in the background thread does not affect the main thread. Actual test found.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.