The earliest use of the queue to hold data, the time node for a one-time read and FTP transmission, but the queue data is too slow, so reuse the memory stream to solve the performance of the data, on the other hand, the data for seamless FTP upload.
1, first set up a memory stream MemoryStream, and build its StreamWriter
MemoryStream data_stream = new MemoryStream ();
StreamWriter data_writer = new StreamWriter (Data_stream);
2, a locked memory stream write to the thread function
int Data_enqueue (string data)
{
lock (data_monitor)
{
try
{
data_writer. WriteLine (data);
Data_writer. Flush ();
return 1;
}
catch (Exception ex)
{
class_log.err_write ("[Data_enqueue]" + ex.) message);
return 0;}}}
3. FTP connection and write the memory stream data directly into the FTP stream
ftpwebrequest ftp = (ftpwebrequest) ftpwebrequest.create (new Uri (URI)); Ftp.
Credentials = new NetworkCredential (Ftp_username, Ftp_password); Ftp.
KeepAlive = false; Specifies that command ftp be executed.
method = WebRequestMethods.Ftp.UploadFile; Specifies the data transfer type FTP.
Usebinary = true; 15 seconds Timeout ftp.
Timeout = 15000; using (Stream Ftp_stream = ftp. GetRequestStream ()) {//Lock Lock (Data_monitor) {//point to Memory stream header Data_stream.
Position = 0; Data_stream.
CopyTo (Ftp_stream); Data_writer.
Close (); Data_stream.
Close ();
Data_stream = new MemoryStream ();
Data_writer = new StreamWriter (Data_stream); } ftp_stream.
Close (); }