Я в некотором роде новичок в C# и, конечно, новичок в асин c, а что нет. Но я замечал всякий раз, когда запускал свой процесс, Process Pro c = new Process (); Пока он работал, все мое приложение было бы заморожено. Что странно, это не происходило раньше, но сейчас это так, и я понятия не имею, почему.
Я немного исследовал асин c и ждал, и я не мог заставить его работать, он просто завис. Вот мой код.
private void but_SD_Click(object sender, RoutedEventArgs e)
{
string checkIP = ConfigurationManager.AppSettings["checkip"];
if (checkIP == "true")
{
string localIP;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address.ToString();
}
if (localIP.StartsWith("10"))
{
StartDownload();
}
}
else
{
StartDownload();
}
}
private void StartDownload()
{
string cmdFull = cmdlab.Text;
string cookie = textCookie.Text;
string output = textOutput.Text;
string URL = tb_URL.Text;
if (output == "")
{
Console.WriteLine("[VDDL] No Output Directory Found...");
MessageBox.Show("[VDDL] Please Select an Output Location");
return;
}
if (format_box.Text != "Default")
{
if (cookie != "")
{
string ForArgs = "";
string Format = format_box.Text.ToLower();
if (Format == "mp3")
{
ForArgs = "-x --audio-format mp3";
}
else if (Format == "mp4")
{
ForArgs = "-f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best";
}
string ALL = System.IO.Path.Combine(cmdFull + " --format " + Format + " --cookies " + cookie + " --output " + output + " " + URL);
Console.WriteLine("[VDDL] Executing Command: " + ALL);
Process p = new Process();
p.StartInfo.FileName = cmdFull;
p.StartInfo.Arguments = ForArgs + " --cookies " + cookie + " --output " + output + " " + URL;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += proc_OutputDataRecieved;
p.ErrorDataReceived += proc_ErrorDataRecieved;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(myProcess_Exited);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
else
{
string ForArgs = "";
string Format = format_box.Text.ToLower();
if (Format == "mp3")
{
ForArgs = "-x --audio-format mp3";
}
else if (Format == "mp4")
{
ForArgs = "-f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best";
}
string Mp3a = System.IO.Path.Combine(cmdFull + " --format " + Format + " --output " + output + " " + URL);
Console.WriteLine("[VDDL] Executing Command: " + Mp3a);
Process pa = new Process();
pa.StartInfo.FileName = cmdFull;
pa.StartInfo.Arguments = ForArgs + " --output " + output + " " + URL;
pa.StartInfo.UseShellExecute = false;
pa.StartInfo.RedirectStandardOutput = true;
pa.StartInfo.RedirectStandardError = true;
pa.StartInfo.CreateNoWindow = true;
pa.OutputDataReceived += proc_OutputDataRecieved;
pa.ErrorDataReceived += proc_ErrorDataRecieved;
pa.EnableRaisingEvents = true;
pa.Exited += new EventHandler(myProcess_Exited);
pa.Start();
pa.BeginOutputReadLine();
pa.BeginErrorReadLine();
}
}
else if (cookie != "")
{
string CookOut = System.IO.Path.Combine(cmdFull + " --cookies " + cookie + " --output " + output + " " + URL);
Console.WriteLine("[VDDL] Executing Command: " + CookOut);
Process pas = new Process();
pas.StartInfo.FileName = cmdFull;
pas.StartInfo.Arguments = "--cookies " + cookie + " --output " + output + " " + URL;
pas.StartInfo.UseShellExecute = false;
pas.StartInfo.RedirectStandardOutput = true;
pas.StartInfo.RedirectStandardError = true;
pas.StartInfo.CreateNoWindow = true;
pas.OutputDataReceived += proc_OutputDataRecieved;
pas.ErrorDataReceived += proc_ErrorDataRecieved;
pas.EnableRaisingEvents = true;
pas.Exited += new EventHandler(myProcess_Exited);
pas.Start();
pas.BeginOutputReadLine();
pas.BeginErrorReadLine();
}
else
{
string Out = System.IO.Path.Combine(cmdFull + " --output " + output + " " + URL);
Console.WriteLine("[VDDL] Executing Command: " + Out);
Process pass = new Process();
pass.StartInfo.FileName = cmdFull;
pass.StartInfo.Arguments = "--output " + output + " " + URL;
pass.StartInfo.UseShellExecute = false;
pass.StartInfo.RedirectStandardOutput = true;
pass.StartInfo.RedirectStandardError = true;
pass.StartInfo.CreateNoWindow = true;
pass.OutputDataReceived += proc_OutputDataRecieved;
pass.ErrorDataReceived += proc_ErrorDataRecieved;
pass.EnableRaisingEvents = true;
pass.Exited += new EventHandler(myProcess_Exited);
pass.Start();
pass.BeginOutputReadLine();
pass.BeginErrorReadLine();
}
}
private void proc_ErrorDataRecieved(object sender, DataReceivedEventArgs e)
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate ()
{
Console.WriteLine(e.Data);
//MessageBox.Show(e.Data);
txtConsole.ScrollToEnd();
}));
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate ()
{
Console.WriteLine("[VDDL] DOWNLOAD COMPLETE...");
Console.WriteLine("\n[VDDL] CHECK OUTPUT DIRECTORY...");
txtConsole.ScrollToEnd();
}));
}
private void proc_OutputDataRecieved(object sender, DataReceivedEventArgs e)
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate ()
{
Console.WriteLine(e.Data);
txtConsole.ScrollToEnd();
}));
}
Также стоит упомянуть, что я иногда заикаюсь или замираю при использовании определенных кнопок. У меня есть ощущение, что это связано с моей консолью / текстовым полем. У меня есть писатель текстового потока, который записывает все console.writelines в текстовое поле. Вот что:
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString()); // When character data is written, append it to the text box.
_output.ScrollToEnd();
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
Любая помощь будет оценена!