打开同步方法异步(FTP /上传) [英] Turning a Synchronous method async (FTP/Upload)

查看:125
本文介绍了打开同步方法异步(FTP /上传)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过FTP将文件上传到我的服务器,但现在不再是1995年了,所以我想我可能想让它在后台进行异步操作或上传文件,以免导致界面无响应。 p>

这个页面有一个通过FTP上传文件的同步方法的完整示例。如何将此变为异步方法?



同步代码:

 使用系统; 
使用System.IO;
使用System.Net;
使用System.Text;

namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
//获取用于与服务器通信的对象。
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftp://www.contoso.com/test.htm);
request.Method = WebRequestMethods.Ftp.UploadFile;

//此示例假定FTP站点使用匿名登录。
request.Credentials = new NetworkCredential(anonymous,janeDoe@contoso.com);

//将文件的内容复制到请求流中。
StreamReader sourceStream = new StreamReader(testfile.txt);
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents,0,fileContents.Length);
requestStream.Close();

FtpWebResponse response =(FtpWebResponse)request.GetResponse();

Console.WriteLine(Upload File Complete,status {0},response.StatusDescription);

response.Close();
}
}
}
}

我只是把它扔进一个BackgroundWorker?



注意事项:

我不需要知道转移/上传的进度。所有我需要知道的是状态(上传或完成)。

解决方案


它成为一个BackgroundWorker?


没有。这些操作是I / O绑定的。您将浪费线程池线程,同时等待下载响应流/读取文件。



您应该考虑使用上面使用的方法的异步版本以及异步 / 等待。这样可以避免浪费线程池线程,而是依靠I / O完成来完成任务。


I need to upload files via FTP to my server, but it's not 1995 anymore so I was thinking that I probably want to make it asynchronous or upload the files in the background so as to not make the UI become unresponsive.

The code from this page has a full example of a synchronous method of uploading files via FTP. How can I turn this into an Asynchronous method?

The synchronous code:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();
            }
        }
    }
}

Should I just throw it into a BackgroundWorker?

things to note:

I don't need to know the progress of the transfer/upload. All I need to know is the status (Uploading or Completed).

解决方案

Should I just throw it into a BackgroundWorker?

No. These sorts of operations are I/O bound. You would be wasting a Thread Pool thread while it waits to download the response stream/read files.

You should look into using the async versions of the methods you've used above and the magic that is async/await. This will save you from wasting a Thread Pool thread and instead will rely on I/O completion to complete the task.

这篇关于打开同步方法异步(FTP /上传)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆