請問 waitObject.WaitOne();是用來干嘛的,上傳檔案的時候執行完這一步就卡死了。
public string UploadAsync(string srcFileFullName, string destNewName)
{
FileInfo fileInf = new FileInfo(srcFileFullName);
if (string.IsNullOrEmpty(destNewName))
{
destNewName = fileInf.Name;
}
string uri = fURI.TrimEnd('/') + "/" + destNewName;
ManualResetEvent waitObject;
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(uri));
request.Credentials = new NetworkCredential(fUserID, fPassword);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.UseBinary = true;
request.UsePassive = ftpUsePassive;
request.ContentLength = fileInf.Length;
// Store the request in the object that we pass into the 將請求存盤在我們傳遞到
// asynchronous operations. 異步操作。
state.Request = request;
state.FileName = srcFileFullName;
// Get the event to wait on. 讓活動繼續等待。
waitObject = state.OperationComplete;
// Asynchronously get the stream for the file contents.異步獲取檔案內容的流。
request.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
state
);
// Block the current thread until all operations are complete.阻塞當前執行緒,直到所有操作完成。
waitObject.WaitOne();
// The operations either completed or threw an exception.操作要么完成,要么拋出例外。
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
return state.StatusDescription;
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState;
Stream requestStream = null;
// End the asynchronous call to get the request stream. 結束異步呼叫以獲取請求流。
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream. 將檔案內容復制到請求流。
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
// IMPORTANT: Close the request stream before sending the request.重要事項: 在發送請求之前關閉請求流。
requestStream.Close();
stream.Close();
// Asynchronously get the response to the upload request. 異步獲取對上載請求的回應。
state.Request.BeginGetResponse(
new AsyncCallback(EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.將例外回傳到主應用程式執行緒。
catch (Exception e)
{
state.OperationException = e;
state.OperationComplete.Set();
return;
}
}
// The EndGetResponseCallback method Endgetresponsecallback 方法
// completes a call to BeginGetResponse. //完成對 beggingetresponse 的呼叫。
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState state = (FtpState)ar.AsyncState;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)state.Request.EndGetResponse(ar);
response.Close();
state.StatusDescription = response.StatusDescription;
// Signal the main application thread that 通知主應用程式執行緒。
// the operation is complete.//操作已完成
state.OperationComplete.Set();
}
// Return exceptions to the main application thread.將例外回傳到主應用程式執行緒。
catch (Exception e)
{
state.OperationException = e;
state.OperationComplete.Set();
}
}
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;
public FtpState()
{
wait = new ManualResetEvent(false);
}
public ManualResetEvent OperationComplete
{
get { return wait; }
}
public FtpWebRequest Request
{
get { return request; }
set { request = value; }
}
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
public Exception OperationException
{
get { return operationException; }
set { operationException = value; }
}
public string StatusDescription
{
get { return status; }
set { status = value; }
}
}
uj5u.com熱心網友回復:
自己頂一下,有大神知道嘛uj5u.com熱心網友回復:
不就是等待執行緒執行完畢么,如果你傳的檔案過大,肯定不是3-5s就結束的,想要等待傳輸結果的話,就會有假死的狀態啊,可以傳的時候異步回傳進度,這樣就慢慢等就是uj5u.com熱心網友回復:
我這個就是異步上傳檔案,你說的異步回傳進度需要怎么操作?
uj5u.com熱心網友回復:
https://blog.csdn.net/baidu_39486224/article/details/79961156大概就像這個,根據上傳的大小,和上傳資料百分比,用action事件回傳進度
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/276482.html
標籤:ASP.NET
