Upload function
Add the default configuration items first (there is a corresponding method for setting this part) :
Asynchronous upload part of the code
// </summary> // <param </ / </ param </ / </ param </ / </ param </ / </ param </ / </ param </ / </ param </ / </ param </ / Name ="fullName"></param> // <param name="fileName"> If it is empty, the default name of the uploaded file is </param> // <param name="process"></param> public void AsynUpload(string fullName,string fileName, Func<FtpState, FtpState> process) { ManualResetEvent waitObject; FtpState state = new FtpState(); try { string _port = string.IsNullOrEmpty(port) ? "" : $":{port}"; fileName = string.IsNullOrEmpty(fileName) ? Path.GetFileName(fullName) : fileName; string ftpfullpath = $"ftp://{ipAddr}{_port }//{fileName}"; Uri target = new Uri(ftpfullpath); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(account, password); //request.KeepAlive = false; Request.UseBinary = true; / / FTP. Default is transferring binary request ServicePoint. ConnectionLimit = maxConnect; // Store the request in the object that we pass into the // asynchronous operations.state. request = request; state.FullName = fullName; state.ProcessCall = process; state.Operate = FtpOperate.UpLoad; FileInfo file = new FileInfo(fullName); state.Size = file.Length; // 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) { if (UploadFail ! = null) { UploadFail.Invoke(state, state.OperationException); return; } throw state.OperationException; } else { if (UploadSuccess ! = null) { UploadSuccess.Invoke(state, state.StatusDescription); return; } } } catch (Exception ex) { if (UploadFail ! = null) { UploadFail.Invoke(state, ex); return; }}}Copy the code
Using multiple threads to upload asynchronously:
If the number of threads in the thread pool is set, the number of threads in each execution can not be limited to 3, because the asynchronous connection is not closed, so I have not thought about how to close it, because creating multiple connections each time will lead to poor performance (frequent multiple connections consume high performance). In my opinion, if all upload tasks are finished, then close the open connection. Finally, if you do this, feel free to leave a comment below. If there are more than 3 threads in the thread pool, they will enter the queue. When there are more threads in the queue, new threads will enter the queue (and so on).
Note: Request. KeepAlive is false, all connections will be closed automatically eventually, but frequent FTP connections will degrade performance, not suitable for too many file uploads.
Download features:
Of course, if it is multithreaded, it needs to be set, because the download is synchronous. ThreadPool.SetMinThreads(1, 1); // Set the minimum number of threads to 1.SetMaxThreads(5, 5); // Set the maximum number of threads to 5. The two methods must be used together to control the number of threads
Effect:
Source code address: gitee.com/ten-ken/per…
Function signal
As for the breakpoint continuation, a little modification on the line, wait for you to discover oh!
Welcome to pay attention to my public number: programmer Ken, the path of the program, let us explore together, common progress.