Upload function

First, add the default configuration item (there are corresponding methods for setting this section) :

Part of the code for asynchronous uploading

// </summary> // <summary> // <param </summary> // <param </summary> // <summary> // <param </summary> // <summary> // <param <param </summary> // <summary> // <summary> // <param <param </summary> // <summary> // <summary> // <summary> < name="fullName"></param> // <param name="fileName"> // <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; // Whether to save the connection Set this does not take effect asynchronously upload request. useBinary = true; / / FTP. Default is transferring binary request ServicePoint. ConnectionLimit = maxConnect; / / maximum number of connections / / 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; }}}

Upload asynchronously using multiple threads:

If you set the number of threads in the thread pool, you can’t control the number of threads in each execution to no more than 3, because the asynchronous connection is not closed, so I haven’t thought of how to close it, because creating multiple connections at a time will lead to poor performance (frequent multiple connections consume high performance). In my opinion, if all uploading tasks are finished, then close the open connection. This is the last. If you have implemented this method, please leave a comment below. The maximum number of FTP connections is set to 3. If there are more than 3 threads in the thread pool, they will enter the queue. After the thread in the queue completes, new threads will enter the queue (and so on).

Note: Request.KeepAlive is false, eventually all connections will be closed automatically, but frequent FTP connections will lose performance, so too many file uploads are not suitable.

Download function:

Of course, if it is multi-threaded also need to set, because the download is synchronous download.

ThreadPool.SetMinThreads(1, 1); // Set the minimum number of threads to 1 threadPool.SetMaxThreads(5, 5); // Set the maximum number of threads to 5. These two methods must be used together to control the number of threads

Effect:

Source address: https://gitee.com/ten-ken/per…

Function signal

As for the break point continuation, slightly modified on the line, waiting for you to find!

Welcome to pay attention to my public number: programmer Ken, the process of the road, let’s explore together, common progress.