Home > Net >  Urgent!! Ftp upload file waitObject. WaitOne ()
Urgent!! Ftp upload file waitObject. WaitOne ()

Time:04-15

Excuse me waitObject. WaitOne (); Is used to do, when uploading files to perform this step card dead,

 

Public string UploadAsync (string srcFileFullName, string destNewName)
{
The FileInfo fileInf=new the 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. The KeepAlive=false;
Request. The Method=WebRequestMethods. Ftp. UploadFile;
Request. The Timeout=300000;
Request. UseBinary=true;
Request. UsePassive=ftpUsePassive;
Request. ContentLength=fileInf. Length;

//Store the request in the object that we pass into the transfer request is stored in the us to
//asynchronous operations. Asynchronous operations,
State. The Request=Request;
State. The FileName=srcFileFullName;

//Get the event to wait on. Let the activity to wait,
WaitObject=state. OperationComplete;

//Asynchronously get the stream for the file contents, asynchronous access to the file content flow,
Request. BeginGetRequestStream (
New AsyncCallback (EndGetStreamCallback),
State
);

//Block the current thread until all operations are complete, Block the current thread, until all the operation is completed,
WaitObject. WaitOne ();

//The operations either completed or threw an exception. The operation or completed, or an exception is thrown,
If (state OperationException!=null)
{
Throw state. OperationException;
}
The else
{
The return state. StatusDescription;
}
}

Private static void EndGetStreamCallback IAsyncResult (ar)
{
FtpState state=(FtpState) ar. AsyncState;

The Stream requestStream=null;
//End the asynchronous call to get the request stream. The End of an asynchronous call to obtain the request flow,
Try
{
RequestStream=state. Request. EndGetRequestStream (ar);
//Copy the file contents to the request stream. Copies the file content to the request flow,
Const int bufferLength=2048;
Byte [] buffer=new byte [bufferLength];
int count=0;
Int readBytes=0;
FileStream stream=File. OpenRead (state. The 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. IMPORTANT: before sending a request to Close the request flow,
RequestStream. Close ();
Stream. The Close ();
//Asynchronously get the response to the upload request. The asynchronous response to upload request,
State. Request. BeginGetResponse (
New AsyncCallback (EndGetResponseCallback),
State
);
}
//Return exceptions to the main application thread. The abnormal Return to the main application thread,
The catch (Exception e)
{
State. OperationException=e;
State. OperationComplete. The Set ();
return;
}

}

//The EndGetResponseCallback method EndGetResponseCallback method
//of a call to BeginGetResponse.//completed calls to beggingetresponse,
Private static void EndGetResponseCallback IAsyncResult (ar)
{
FtpState state=(FtpState) ar. AsyncState;
FtpWebResponse response=null;
Try
{
The response=(FtpWebResponse) state. Request. EndGetResponse (ar);
The response. The Close ();
State. StatusDescription=response. StatusDescription;
//Signal to the main application thread that inform the main application thread,
//the operation is complete.//operation has been completed
State. OperationComplete. The Set ();
}
//Return exceptions to the main application thread. The abnormal Return to the main application thread,
The catch (Exception e)
{
State. OperationException=e;
State. OperationComplete. The 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
{
The get {return wait; }
}

Public FtpWebRequest Request
{
The get {return request; }
The set {request=value; }
}

Public string FileName
{
The get {return fileName; }
The set {fileName=value; }
}
Public Exception OperationException
{
The get {return operationException; }
The set {operationException=value; }
}
Public string StatusDescription
{
The get {return status; }
The set {status=value; }
}
}

CodePudding user response:

Oneself the top, there is a great god know

CodePudding user response:

Not yao is waiting thread has been completed, if you send the file is too large, certainly is not the end of 3-5 s, want to wait for transfer results, there will be a state of suspended animation, can pass the asynchronous return schedule, so it is slowly, etc

CodePudding user response:

nullnullnullnullnullnullnullnullnullnull
  • Related