开发者

C# Process - Pause or sleep before completion

I have a Process:

Process pr = new Process();
pr.StartInfo.FileName = @"wput.exe";
pr.StartInfo.Arguments = @"C:\Downloads\ ftp://user:dvm@172.29.200.158/Transfer/Updates/";
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.UseShellExecute = fa开发者_Go百科lse;
pr.StartInfo.
pr.Start();

string output = pr.StandardOutput.ReadToEnd();

Console.WriteLine("Output:");
Console.WriteLine(output);

Wput is an ftp upload client.

At the moment when I run the process and begin the upload, the app freezes and the console output won't show until the end. I guess the first problem is solvable by using a Thread.

What I want to do is start an upload, have it pause every so often, read whatever output has been generated (use this data do make progress bar etc), and begin again.

What classes/methods should I be looking into?


You can use the OutputDataReceived event to print the output asynchronously. There are a few requirements for this to work:

The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.

An example of this working is below. It's just doing a long running operation that also has some output (findstr /lipsn foo * on C:\ -- look for "foo" in any file on the C drive). The Start and BeginOutputReadLine calls are non-blocking, so you can do other things while the console output from your FTP application rolls in.

If you ever want to stop reading from the console, use the CancelOutputRead/CancelErrorRead methods. Also, in the example below, I'm handling both standard output and error output with a single event handler, but you can separate them and deal with them differently if needed.

using System;
using System.Diagnostics;

namespace AsyncConsoleRead
{
    class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            p.StartInfo.FileName = "findstr.exe";
            p.StartInfo.Arguments = "/lipsn foo *";
            p.StartInfo.WorkingDirectory = "C:\\";
            p.StartInfo.UseShellExecute = false;

            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);

            p.Start();

            p.BeginOutputReadLine();

            p.WaitForExit();
        }

        static void OnDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}


The best method would be to use libraries which support FTP, instead of relying on external applications. If you don't need much info from the external application and are not verifying their outputs, then go ahead. Else better use FTP client libs.

May be you would like to see libs/documentations:

http://msdn.microsoft.com/en-us/library/ms229711.aspx

http://www.codeproject.com/KB/IP/ftplib.aspx

http://www.c-sharpcorner.com/uploadfile/danglass/ftpclient12062005053849am/ftpclient.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜