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.