Вот пример функции:
Код: Выделить всё
public static string GetYTTitle(string link)
{
//link example:
//https://www.youtube.com/watch?v=3QxwYWYzEis&list=RDEMF3TMXnvKDsn8vLgAp5nmdA&index=9
string standard_output = "";
int n = 0;
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = @"D:\youtube-dl\yt-dlp.exe";
p.StartInfo.Arguments = " --get-title " + link;
p.ErrorDataReceived += (s, e) =>
{
if (e != null)
{
Console.WriteLine(" error Ouput Data ");
Console.WriteLine(e.Data);
}
};
p.OutputDataReceived += (s, e) =>
{
if (e != null)
{
Console.WriteLine("Datos de Standard Ouput: ");
Console.WriteLine(e.Data);
standard_output += e.Data;
if (standard_output.Length > 100)
{
Console.WriteLine("Enough charactes gotten");
p.ErrorDataReceived -= (s, e)=> { };
p.OutputDataReceived -= (s, e) => { };
p.Kill();
}
}
};
p.Exited += (s, e) =>
{
Console.WriteLine("Process finished.");
p.ErrorDataReceived -= (s, e) => { };
p.OutputDataReceived -= (s, e) => { };
p.Kill();
};
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
while (!p.HasExited)
{
Thread.Sleep(1000);
n = n+1; ;
Console.WriteLine(n + " seconds elapsed of " +40);
Console.ResetColor();
if (n > 39)
{
p.ErrorDataReceived -= (s, e) => { };
p.OutputDataReceived -= (s, e) => { };
p.Kill();
return standard_output;
}
}
return standard_output;
}
Я отписался от обработчиков событий и завершил процесс перед выходом из функции. Однако данные по-прежнему принимаются в OutputDataReceived и ErrorDataReceived, даже если программа уже вышла из функции GetYTTitle(). Как мне перестать получать эти бесконечные данные в обработчиках событий?
Подробнее здесь: https://stackoverflow.com/questions/784 ... -the-event
Мобильная версия