提示コードの///
内部のコードの//ここです。
のBeginOutputReadLine()
関数ですがコンストラクタにてイベントを利用する変数にtrue
を設定しているのですがOutput_Ttile();
イベント関数が実行されません。これはなぜでしょうか?参考サイト参考に同じようにやりましたが実行されません。
環境
OS windows 10
IDE Visual studio 2022
windows form
参考サイト
1,https://www.codeproject.com/Questions/3133365/How-to-turn-this-code-to-async-mode-because-my-pro
2,https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?redirectedfrom=MSDN&view=net-6.0#System_Diagnostics_Process_WaitForExit
3,https://qiita.com/skitoy4321/items/10c47eea93e5c6145d48
Data.cs
cs
1using System.Diagnostics; 2 3namespace Downloader 4{ 5 6 public enum CodecType 7 { 8 Audio, 9 Movie, 10 Audio_Movie 11 12 } 13 14 15 public struct Format 16 { 17 public string? id; 18 public string? extension; 19 public string? resolution; 20 public string? special_Resolution; //両方あるときは音声 21 22 public float? size; 23 public CodecType codecType; 24 25 } 26 27 public class Data 28 { 29 private Process process = new Process(); 30 31 private string? url = null; 32 private string? title = null; 33 private List<Format>? format; 34 private string? format_audio = null; 35 private string? format_movie = null; 36 private string? format_extension = null; 37 38 /* ########################### 初期化 ###########################*/ 39 public Data() 40 { 41 process.StartInfo.FileName = "yt-dlp"; 42 process.StartInfo.RedirectStandardOutput = true; 43 process.StartInfo.RedirectStandardError = true; 44 process.StartInfo.RedirectStandardInput = true; 45 46 process.StartInfo.CreateNoWindow = true; // コンソール・ウィンドウを開かない 47 process.StartInfo.UseShellExecute = false; // シェル機能を使用しない 48 process.EnableRaisingEvents = true; 49 //process.OutputDataReceived += this.Output; //ログ 50 process.ErrorDataReceived += this.Output_Error; //エラー 51 } 52 53 private void Output_Title(object sender, DataReceivedEventArgs e) 54 { 55 Debug.WriteLine("らららららら"); 56 57 if(e.Data != null) 58 { 59 60 title = e.Data.ToString(); 61 Debug.WriteLine("title " + title); 62 } 63 else 64 { 65 Debug.WriteLine("null"); 66 67 } 68 } 69 70 /* ########################### URLからフォーマットを取得 非同期 ###########################*/ 71 public void setData(string u) 72 { 73 //タイトルを取得 74/////////////////////////////////////////////////////////////////////////////////////////////////////////// 75 u = "\"" + u + "\""; 76 77 url = u; //URLを設定 78 Debug.WriteLine("ID " + u); 79 80 process.OutputDataReceived += new DataReceivedEventHandler(this.Output_Title); 81 process.ErrorDataReceived += this.Output_Title; 82 //process.Exited += this.Output_Title; 83 process.StartInfo.Arguments = " --no-warnings --get-title " + u + " -o " + "\"%(title)s\""; 84 process.Start(); 85 Debug.WriteLine("rrrr"); 86 87 88 process.BeginOutputReadLine(); //ここです。 89 process.BeginErrorReadLine(); 90 91 Debug.WriteLine("えええええええ"); 92 93 process.WaitForExit(); 94 Debug.WriteLine("END"); 95/////////////////////////////////////////////////////////////////////////////////////////////////////////// 96 // 使い終わったら止める 97 process.CancelOutputRead(); 98 process.CancelErrorRead(); 99 100 101 102 //フォーマットを取得 103 process.OutputDataReceived -= this.Output_Title; 104 process.OutputDataReceived += this.Output; //ログ 105 106 process.StartInfo.Arguments = " -F " + u; 107 process.Start(); 108 109 process.BeginOutputReadLine(); 110 process.BeginErrorReadLine(); 111 112 113 process.WaitForExit(); 114 115 // 使い終わったら止める 116 process.CancelOutputRead(); 117 process.CancelErrorRead(); 118 119 } 120 121 /* ########################### エラー出力 ###########################*/ 122 public void Output_Error(object sender, DataReceivedEventArgs e) 123 { 124 Debug.WriteLine(e.Data.ToString()); 125 } 126 127 128 129 }; 130 131}
あなたの回答
tips
プレビュー