提示コードの///
コメント部内部のコードですがマルチスレッドですべてのスレッドが終了するまで待機してすべて終わったら次のコードに推移するという処理を書きたいのですが実装方法がわかりません。マルチタスクでいうTask.WhenAll
のようなことがしたいです。現状では一つのスレッドですべての処理を行っています。このソフトは音楽ファイルをffmpeg
でflac
を.m4a
に変換する処理なのですがマルチスレッドで素早く終らせる方法が知りたいです。
cs
1using System; 2using System.Diagnostics; 3using System.IO; 4 5public class Program 6{ 7 static List<string> dirList = new List<string>(); //ディレクトリパスリスト 8 static List<string> filePathList = new List<string>(); 9 10 11 /*######################################################## 12 * サブディレクトリを再帰的に取得して設定 13 #########################################################*/ 14 private static void GetSubFolderPathList(string rootPath) 15 { 16 foreach (string path in Directory.GetDirectories(rootPath)) 17 { 18 if (System.IO.Directory.Exists(path) == true) 19 { 20 dirList.Add(path); 21 GetSubFolderPathList(path); 22 } 23 } 24 } 25 26 /*######################################################## 27 * ディレクトリからファイル名を取得 28 #########################################################*/ 29 public static List<string> GetFilePathList(string dirPath) 30 { 31 List<string> list = new List<string>(); 32 foreach (string path in Directory.GetFiles(dirPath)) 33 { 34 //Console.WriteLine("あああ"); 35 list.Add(path); 36 } 37 38 return list; 39 } 40 41 /*######################################################## 42 * 全てのファイルパスを取得 43 #########################################################*/ 44 private static List<string> GetAllFilePath(string rootPath) 45 { 46 GetSubFolderPathList(rootPath); //サブディレクトリー取得 47 48 //Console.WriteLine(" " + dirList.Count()); 49 50 51 List<string> file = new List<string>(); 52 53 if(dirList.Count() == 0) 54 { 55 dirList.Add(rootPath); 56 } 57 58 foreach (string path in dirList) 59 { 60 List<string> list = GetFilePathList(path); 61 file.AddRange(list); 62 } 63 64 return file; 65 } 66 67 static int threadNum = 0; 68 static int threadCount = 0; 69 70 private static async void Run(string filepath) 71 { 72 73 Process process = new Process(); 74 process.StartInfo.FileName = "ffmpeg.exe"; 75 process.EnableRaisingEvents = true; 76 process.StartInfo.CreateNoWindow = true; //コンソール・ウィンドウを開かない 77 process.StartInfo.UseShellExecute = false; //シェル機能を使用しない 78 process.StartInfo.RedirectStandardOutput = true; 79 process.StartInfo.RedirectStandardError = true; 80 81 82 83 string inputPath = filepath; 84 inputPath = "\"" + inputPath + "\""; 85 string outputPath = Path.ChangeExtension(filepath, ".m4a"); 86 outputPath = "\"" + outputPath + "\""; 87 88 //Console.WriteLine("あああ " + Path.ChangeExtension(str, ".m4a")); 89 process.StartInfo.Arguments = " -y -i " + inputPath + " -c:a alac -c:v copy " + outputPath; 90 91 Console.WriteLine(process.StartInfo.Arguments); 92 93 process.Start(); 94 95 //process.WaitForExit(); 96 //threadNum++; 97 Console.WriteLine("ああああ"); 98 99 } 100 101 102 public static void Main() 103 { 104 105 106 string rootPath; 107 Console.WriteLine("input folder path: "); 108 //rootPath = Console.ReadLine().Trim('\"'); 109 rootPath = "C:\\Users\\yw325\\Desktop\\Blooming Days"; 110 111 filePathList = GetAllFilePath(rootPath); //すべてのファイルパスを取得 112 113 // Console.WriteLine("filePathList.Count() " + filePathList.Count()); 114 115 116////////////////////////////////////////////////////////////////////////////////////////////// 117 List<Thread> thread = new List<Thread>(); 118 119 Thread t = new Thread(new ThreadStart(() => 120 { 121 foreach (string str in filePathList) 122 { 123 Run(str); 124 } 125 126 })); 127 128 129 t.Start(); 130 t.Join(); 131////////////////////////////////////////////////////////////////////////////////////////////// 132 Console.WriteLine("終了"); 133 134 135 136 137 } 138 139} 140
回答に記載しました。
回答1件
あなたの回答
tips
プレビュー