2011-01-31 54 views
0

如何暫停和恢復FTP上傳過程? 我的上傳過程如下代碼。如何實現暫停並恢復過程?如何暫停和恢復WebRequestMethods.Ftp.UploadFile過程?

FileInfo fileInf = new FileInfo(filename); 
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + host + defaultDir + "/" + fileInf.Name)); 

reqFTP.Credentials = new NetworkCredential(user, pass); 
reqFTP.KeepAlive = false; 
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
reqFTP.UseBinary = true; 
reqFTP.ContentLength = fileInf.Length; 

int buffLength = 2048; 
byte[] buff = new byte[buffLength]; 
int contentLen; 
FileStream fs = fileInf.OpenRead(); 
Stream strm = reqFTP.GetRequestStream(); 
contentLen = fs.Read(buff, 0, buffLength); 
int maxLen = contentLen; 
while (contentLen != 0) 
{ 
       // Write Content from the file stream to the FTP Upload Stream 
       strm.Write(buff, 0, contentLen); 
       contentLen = fs.Read(buff, 0, buffLength); 
} 

strm.Close(); 
fs.Close(); 

謝謝。

回答

1

你想實現一個異步任務。

首先,閱讀這篇文章: http://aspalliance.com/1778

通過使用異步任務你可以暫停和/或恢復 後臺線程,並讓該線程處理文件上傳 請求結束,節省IIS線程池中的插槽。

暫停和恢復功能將通過一些同步邏輯來實現。

例如,可以保存在某個地方的異步任務 - 原工藝 - 標識符, 並準備存儲在數據庫中,文件或任何存儲 爲您提供一些布爾標誌,並在上傳循環,檢查每次迭代中 它有權繼續。

如果它沒有該權限,可以使用監視器,互斥鎖或任何其他線程同步方法來等待「脈衝」,以繼續上傳過程或終止上傳過程。