提示コードですがConverter.csの関数GetFormat(String? url)
部のpro.StandardOutput.ReadToEnd();
の部分で処理が停止してしまいますこれは何が原因なのでしょうか?
環境
OS: windows10
IDE: Visual studio 2022
フレームワーク: .Net 6.0
確認したこと
MainWindow.csの初期化部ですが先にウインドウフォームの初期化を行ってその後Convert.Init()関数を実行してConverterクラスを初期化してその際にpro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true;
をしてStandardOutput.ReadToEnd();
関数を使えるよにしています。Debug.WriteLine()
関数(出力画面参照)でInit関数が先に実行されていることを確認しました。Converter.GetFormat()
関数をコメントアウトするとウインドウが表示されるためこの関数が原因なのは確実です。
参考サイト:https://atmarkit.itmedia.co.jp/fdotnet/dotnettips/805pipeasync/pipeasync.html
参考サイト(リファレンス): https://docs.microsoft.com/ja-jp/dotnet/api/system.diagnostics.process.standardoutput?view=net-6.0
出力画面
'Downloader.exe' (CoreCLR: DefaultDomain): 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.5\System.Private.CoreLib.dll' が読み込まれました。シンボルの読み込みをスキップしました。モジュールは最適化されていて、デバッグ オプションの [マイ コードのみ] 設定が有効になっています。 //文字数の関係で省略 'Downloader.exe' (CoreCLR: clrhost): 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.5\System.Diagnostics.Process.dll' が読み込まれました。シンボルの読み込みをスキップしました。モジュールは最適化されていて、デバッグ オプションの [マイ コードのみ] 設定が有効になっています。 Init 'Downloader.exe' (CoreCLR: clrhost): 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.5\System.Text.Encoding.Extensions.dll' が読み込まれました。シンボルの読み込みをスキップしました。モジュールは最適化されていて、デバッグ オプションの [マイ コードのみ] 設定が有効になっています。 あああああ プログラム '[11572] Downloader.exe' はコード 4294967295 (0xffffffff) で終了しました。
MainWindow.cs
cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Diagnostics; 7using System; 8using System.IO; 9 10namespace Downloader 11{ 12 public partial class MainWindow : Form 13 { 14 15 16 17 public MainWindow() 18 { 19 InitializeComponent(); //ウインドウ初期化 20 21 Converter.Init(); //初期化 22 23 Format? f = Converter.GetFormat("https://www.youtube.com/watch?v=0O7Wc9OgslY&list=RDzCmdKyNYDTw&index=5"); 24 25 26 } 27 28 29 } 30} 31
Converter.cs
cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Diagnostics; 7using System; 8using System.IO; 9 10namespace Downloader 11{ 12 //public static class Function 13 static public class Converter 14 { 15 private static Process pro = new Process(); 16 17 /* ########################### 初期化 ###########################*/ 18 static public void Init() 19 { 20 Debug.WriteLine("Init"); 21 pro.StartInfo.FileName = "yt-dlp "; 22 pro.StartInfo.RedirectStandardOutput = true; 23 pro.StartInfo.RedirectStandardError = true; 24 pro.StartInfo.CreateNoWindow = true; // コンソール・ウィンドウを開かない 25 pro.StartInfo.UseShellExecute = false; // シェル機能を使用しない 26 27 } 28 29 /* ########################### URLからフォーマット取得 ###########################*/ 30 static public Format? GetFormat(String? url) 31 { 32 Format? data = new Format(); 33 34 pro.StartInfo.Arguments = " -F " + url; 35 //Debug.WriteLine("いい"); 36 37 pro.Start(); 38 39 //StreamReader reader = pro.StandardOutput; 40 //string output = reader.ReadToEnd(); 41 42 //Debug.WriteLine("おおお"); 43 //Debug.WriteLine(output); 44 //pro.WaitForExit(); 45 Debug.WriteLine("あああああ"); 46 47 string result = pro.StandardOutput.ReadToEnd(); 48 Debug.WriteLine("qqqqq"); 49 50 if (result == "") 51 { 52 MessageBox.Show("URL ERROR", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 53 54 return null; 55 } 56 else 57 { 58 Debug.WriteLine("ううう"); 59 60 /* 61 StringReader sr = new StringReader(result); 62 string? line = sr.ReadLine(); 63 while (line != null) 64 { 65 66 Debug.WriteLine(line); 67 68 line = sr.ReadLine(); 69 70 } 71 72 sr.Close(); 73 */ 74 75 } 76 77 Debug.WriteLine("いい"); 78 79 80 return null; 81 } 82 83 /* ########################### 指定した文字以降を削除 ###########################*/ 84 static public string GetRemoveRight(string str, string removeStr) 85 { 86 int length = str.IndexOf(removeStr); 87 if (length < 0) 88 { 89 return str; 90 } 91 92 return str.Substring(0, length); 93 } 94 95 96 97 } 98} 99


回答1件
あなたの回答
tips
プレビュー