ADBaseLibrary.Helpers.ShellParser.Start C# (CSharp) Method

Start() public method

public Start ( string path, string arguments, CancellationToken token ) : Task
path string
arguments string
token System.Threading.CancellationToken
return Task
        public async Task Start(string path, string arguments, CancellationToken token)
        {
            await Task.Run(() =>
            {
                _finished = false;
                process = new Process();
                process.StartInfo.FileName = path;
                process.StartInfo.Arguments = arguments;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.EnableRaisingEvents = true;
                process.ErrorDataReceived += (a, e) =>
                {
                    if ((e == null) || (e.Data == null))
                        return;
                    string str = e.Data;
                    if (str.Length > 0)
                        DoError(str);
                };
                process.OutputDataReceived += (a, e) =>
                {
                    if ((e == null) || (e.Data == null))
                        return;
                    string str = e.Data; //.Replace("\r\n", string.Empty);
                    if (str.Length > 0)
                        DoLine(str);

                };
                process.Exited += (a, b) =>
                {
                    _finished = true;
                };
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                bool ret = false;
                do
                {
                    Thread.Sleep(100);
                    if (token.IsCancellationRequested)
                    {
                        process.Kill();
                        token.ThrowIfCancellationRequested();
                    }
                } while (!_finished);
                DoFinish();
            },token);
        }

Usage Example

        public async Task<string> ReMux(string inputfile, string inputs, string subtileargs, Format formats,
            string audiolanguagecode, string audiolanguage, double initialPercent, double percentIncrement,
            DownloadInfo dinfo, IProgress<DownloadInfo> progress, CancellationToken token)
        {
            int formatcnt = 0;
            foreach (Format fol in Enum.GetValues(typeof(Format)))
            {
                if ((fol & formats) == fol)
                {
                    formatcnt++;
                }
            }

            percentIncrement /=(2*formatcnt);
            string size = string.Empty;
            string intermediatefile = dinfo.FullPath + ".tm2";
            foreach (Format fol in Enum.GetValues(typeof(Format)))
            {
                if ((fol & formats) == fol)
                {
                    string ffmpegargs = string.Format(LibSet[FFMPEGArgsS], inputfile, inputs, subtileargs, intermediatefile, fol == Format.Mkv ? string.Empty : "-c:s mov_text ", audiolanguagecode, audiolanguage, fol == Format.Mkv ? "-f matroska" : "-f mp4");
                    token.ThrowIfCancellationRequested();
                    dinfo.Percent = initialPercent;
                    initialPercent += percentIncrement;
                    dinfo.Status = "Muxing Video";
                    progress.Report(dinfo);
                    ShellParser ffm = new ShellParser();
                    await ffm.Start(LibSet[FFMPEGEXES], ffmpegargs, token);
                    dinfo.Percent = initialPercent;
                    initialPercent += percentIncrement;
                    dinfo.Status = "Unique Hashing";
                    progress.Report(dinfo);

                    if (fol == Format.Mkv)
                        Matroska.Matroska.MatroskaHash(intermediatefile, dinfo.FullPath + "." + fol.ToExtension());
                    else
                        Mp4.Mp4.Mp4Hash(intermediatefile, dinfo.FullPath + "." + fol.ToExtension());
                    try
                    {
                        File.Delete(intermediatefile);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                    FileInfo f = new FileInfo(dinfo.FullPath + "." + fol.ToExtension());
                    if (formatcnt == 1)
                    {
                        size = f.Length.ToString();
                    }
                    else
                    {
                        size += f.Length.ToString() + " (" + fol.ToExtension() + "), ";
                    }
                }
            }
            try
            {
                File.Delete(inputfile);
            }
            catch (Exception)
            {
                // ignored
            }
            if (formatcnt > 1)
                size = size.Substring(0, size.Length - 2);
            return size;
        }