diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index e6b519f..015fd42 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -249,38 +249,59 @@ namespace FileConverter private void ConvertFiles() { Thread[] jobThreads = new Thread[this.numberOfConversionThread]; - - // TODO: add dependency between conversion jobs. - - for (int jobIndex = 0; jobIndex < this.conversionJobs.Count; jobIndex++) + + while (true) { - ConversionJob conversionJob = this.conversionJobs[jobIndex]; - if (conversionJob.State != ConversionJob.ConversionState.Ready) + // Compute conversion flags. + ConversionFlags conversionFlags = ConversionFlags.None; + bool allJobAreFinished = true; + for (int jobIndex = 0; jobIndex < this.conversionJobs.Count; jobIndex++) { - continue; + ConversionJob conversionJob = this.conversionJobs[jobIndex]; + allJobAreFinished &= !(conversionJob.State == ConversionJob.ConversionState.Ready || + conversionJob.State == ConversionJob.ConversionState.InProgress); + + if (conversionJob.State == ConversionJob.ConversionState.InProgress) + { + conversionFlags |= conversionJob.StateFlags; + } } - Thread jobThread = null; - while (jobThread == null) + if (allJobAreFinished) { - for (int threadIndex = 0; threadIndex < jobThreads.Length; threadIndex++) + break; + } + + // Start job if possible. + for (int jobIndex = 0; jobIndex < this.conversionJobs.Count; jobIndex++) + { + ConversionJob conversionJob = this.conversionJobs[jobIndex]; + if (conversionJob.State == ConversionJob.ConversionState.Ready && + conversionJob.CanStartConversion(conversionFlags)) { - Thread thread = jobThreads[threadIndex]; - if (thread == null || !thread.IsAlive) + // Find a thread to execute the job. + Thread jobThread = null; + for (int threadIndex = 0; threadIndex < jobThreads.Length; threadIndex++) { - jobThread = new Thread(this.ExecuteConversionJob); - jobThreads[threadIndex] = jobThread; - break; + Thread thread = jobThreads[threadIndex]; + if (thread == null || !thread.IsAlive) + { + jobThread = new Thread(this.ExecuteConversionJob); + jobThreads[threadIndex] = jobThread; + break; + } } - } - if (jobThread == null) - { - Thread.Sleep(50); + if (jobThread != null) + { + jobThread.Start(conversionJob); + } + + break; } } - jobThread.Start(conversionJob); + Thread.Sleep(50); } #if !DEBUG diff --git a/Application/FileConverter/ConversionJobs/ConversionFlags.cs b/Application/FileConverter/ConversionJobs/ConversionFlags.cs new file mode 100644 index 0000000..a78abcd --- /dev/null +++ b/Application/FileConverter/ConversionJobs/ConversionFlags.cs @@ -0,0 +1,14 @@ +// // License: http://www.gnu.org/licenses/gpl.html GPL version 3. +namespace FileConverter.ConversionJobs +{ + /// + /// This enum describe the special conversion state that need to be multi-threads prerequisites. + /// + [System.Flags] + public enum ConversionFlags + { + None = 0x00, + + CdaExtraction = 0x01, + } +} \ No newline at end of file diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index ce07bcc..a424a8d 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -120,6 +120,12 @@ namespace FileConverter.ConversionJobs } } + public ConversionFlags StateFlags + { + get; + protected set; + } + protected virtual InputPostConversionAction InputPostConversionAction { get @@ -133,6 +139,11 @@ namespace FileConverter.ConversionJobs } } + public virtual bool CanStartConversion(ConversionFlags conversionFlags) + { + return true; + } + public void PrepareConversion(string inputFilePath, string outputFilePath = null) { if (string.IsNullOrEmpty(inputFilePath)) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_ExtractCDA.cs b/Application/FileConverter/ConversionJobs/ConversionJob_ExtractCDA.cs index 8cea5e0..8e97cbf 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_ExtractCDA.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_ExtractCDA.cs @@ -38,6 +38,11 @@ namespace FileConverter.ConversionJobs } } + public override bool CanStartConversion(ConversionFlags conversionFlags) + { + return (conversionFlags & ConversionFlags.CdaExtraction) == 0; + } + protected override void Initialize() { base.Initialize(); @@ -105,6 +110,8 @@ namespace FileConverter.ConversionJobs this.compressionConversionJob = ConversionJobFactory.Create(this.ConversionPreset, this.intermediateFilePath); this.compressionConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath); this.compressionThread = new Thread(this.CompressAsync); + + this.StateFlags = ConversionFlags.CdaExtraction; } protected override void Convert() @@ -150,6 +157,8 @@ namespace FileConverter.ConversionJobs this.cdDrive.Close(); + this.StateFlags = ConversionFlags.None; + if (!File.Exists(this.intermediateFilePath)) { this.ConversionFailed("Extraction failed."); diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 2c91e0e..fdd64e9 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -83,6 +83,7 @@ EncodingQualitySliderControl.xaml +