Add prerequisite on conversion job to not be executed at the same time.

This commit is contained in:
Tichau committed 2015-12-07 08:40:22 +01:00
1 parent aad9deb7e6
commit dd1d5da176
5 files changed
+76 -20

No files matched your search

+41 -20
View File
@@ -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
@@ -0,0 +1,14 @@
// // <copyright file="ConversionFlags.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverter.ConversionJobs
{
/// <summary>
/// This enum describe the special conversion state that need to be multi-threads prerequisites.
/// </summary>
[System.Flags]
public enum ConversionFlags
{
None = 0x00,
CdaExtraction = 0x01,
}
}
@@ -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))
@@ -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.");
@@ -83,6 +83,7 @@
<Compile Include="Controls\EncodingQualitySliderControl.xaml.cs">
<DependentUpon>EncodingQualitySliderControl.xaml</DependentUpon>
</Compile>
<Compile Include="ConversionJobs\ConversionFlags.cs" />
<Compile Include="ConversionJobs\ConversionJob_ExtractCDA.cs" />
<Compile Include="ConversionJobs\ConversionJobFactory.cs" />
<Compile Include="ConversionJobs\ConversionJob_FFMPEG.Converters.cs" />