Improve application reactivity.

The conversion preparation is now threaded since it can take a big amount of time to do it (for document conversions)
This commit is contained in:
Tichau committed 2016-09-07 08:03:14 +02:00
1 parent 7daa49e95d
commit b01d577f6b
13 files changed
+64 -38

No files matched your search

@@ -341,8 +341,7 @@ namespace FileConverter
{
string inputFilePath = filePaths[index];
ConversionJob conversionJob = ConversionJobFactory.Create(conversionPreset, inputFilePath);
conversionJob.PrepareConversion(inputFilePath);
this.conversionJobs.Add(conversionJob);
}
}
@@ -358,8 +357,14 @@ namespace FileConverter
private void ConvertFiles()
{
Thread[] jobThreads = new Thread[this.numberOfConversionThread];
// Prepare conversions.
for (int index = 0; index < this.ConvertionJobs.Count; index++)
{
this.ConvertionJobs[index].PrepareConversion();
}
// Convert!
Thread[] jobThreads = new Thread[this.numberOfConversionThread];
while (true)
{
// Compute conversion flags.
@@ -25,17 +25,26 @@ namespace FileConverter.ConversionJobs
{
this.State = ConversionState.Unknown;
this.ConversionPreset = null;
this.initialInputPath = string.Empty;
this.InputFilePath = string.Empty;
}
public ConversionJob(ConversionPreset conversionPreset) : this()
public ConversionJob(ConversionPreset conversionPreset, string inputFilePath) : this()
{
if (conversionPreset == null)
{
throw new ArgumentNullException("conversionPreset");
throw new ArgumentNullException(nameof(conversionPreset));
}
if (string.IsNullOrEmpty(inputFilePath))
{
throw new ArgumentNullException(nameof(inputFilePath));
}
this.initialInputPath = inputFilePath;
this.InputFilePath = inputFilePath;
this.ConversionPreset = conversionPreset;
this.UserState = Properties.Resources.ConversionStatePrepareConversion;
}
public event PropertyChangedEventHandler PropertyChanged;
@@ -66,7 +75,7 @@ namespace FileConverter.ConversionJobs
{
get
{
if (this.OutputFilePaths == null)
if (this.OutputFilePaths == null || this.OutputFilePaths.Length == 0)
{
return string.Empty;
}
@@ -210,19 +219,16 @@ namespace FileConverter.ConversionJobs
return (conversionFlags & ConversionFlags.CdDriveExtraction) == 0;
}
public void PrepareConversion(string inputFilePath, params string[] outputFilePaths)
public void PrepareConversion(params string[] outputFilePaths)
{
if (string.IsNullOrEmpty(inputFilePath))
{
throw new ArgumentNullException(nameof(inputFilePath));
}
if (this.ConversionPreset == null)
{
throw new Exception("The conversion preset must be valid.");
}
string extension = System.IO.Path.GetExtension(inputFilePath);
this.InputFilePath = this.initialInputPath;
string extension = System.IO.Path.GetExtension(this.initialInputPath);
extension = extension.Substring(1, extension.Length - 1);
string extensionCategory = Helpers.GetExtensionCategory(extension);
if (!Helpers.IsOutputTypeCompatibleWithCategory(this.ConversionPreset.OutputType, extensionCategory))
@@ -231,9 +237,6 @@ namespace FileConverter.ConversionJobs
return;
}
this.initialInputPath = inputFilePath;
this.InputFilePath = inputFilePath;
this.OutputFilePaths = outputFilePaths;
if (this.OutputFilePaths.Length == 0)
{
@@ -249,7 +252,7 @@ namespace FileConverter.ConversionJobs
continue;
}
string path = this.ConversionPreset.GenerateOutputFilePath(inputFilePath, index + 1, this.OutputFilePaths.Length);
string path = this.ConversionPreset.GenerateOutputFilePath(this.initialInputPath, index + 1, this.OutputFilePaths.Length);
if (!PathHelpers.IsPathValid(path))
{
@@ -267,7 +270,7 @@ namespace FileConverter.ConversionJobs
string inputExtension = System.IO.Path.GetExtension(this.InputFilePath);
string pathWithoutExtension = this.InputFilePath.Substring(0, this.InputFilePath.Length - inputExtension.Length);
this.InputFilePath = PathHelpers.GenerateUniquePath(pathWithoutExtension + "_TEMP" + inputExtension);
System.IO.File.Move(inputFilePath, this.InputFilePath);
System.IO.File.Move(this.initialInputPath, this.InputFilePath);
}
}
@@ -10,36 +10,36 @@ namespace FileConverter.ConversionJobs
inputFileExtension = inputFileExtension.ToLowerInvariant().Substring(1, inputFileExtension.Length - 1);
if (inputFileExtension == "cda")
{
return new ConversionJob_ExtractCDA(conversionPreset);
return new ConversionJob_ExtractCDA(conversionPreset, inputFilePath);
}
if (inputFileExtension == "doc" || inputFileExtension == "docx" || inputFileExtension == "odt")
{
return new ConversionJob_Word(conversionPreset);
return new ConversionJob_Word(conversionPreset, inputFilePath);
}
if (conversionPreset.OutputType == OutputType.Ico)
{
return new ConversionJob_Ico(conversionPreset);
return new ConversionJob_Ico(conversionPreset, inputFilePath);
}
if (conversionPreset.OutputType == OutputType.Gif)
{
return new ConversionJob_Gif(conversionPreset);
return new ConversionJob_Gif(conversionPreset, inputFilePath);
}
if (conversionPreset.OutputType == OutputType.Pdf)
{
return new ConversionJob_ImageMagick(conversionPreset);
return new ConversionJob_ImageMagick(conversionPreset, inputFilePath);
}
if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image ||
Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document)
{
return new ConversionJob_ImageMagick(conversionPreset);
return new ConversionJob_ImageMagick(conversionPreset, inputFilePath);
}
return new ConversionJob_FFMPEG(conversionPreset);
return new ConversionJob_FFMPEG(conversionPreset, inputFilePath);
}
}
}
@@ -25,7 +25,7 @@ namespace FileConverter.ConversionJobs
this.IsCancelable = true;
}
public ConversionJob_ExtractCDA(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_ExtractCDA(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
this.IsCancelable = true;
}
@@ -105,7 +105,7 @@ namespace FileConverter.ConversionJobs
// Sub conversion job (for compression).
this.compressionConversionJob = ConversionJobFactory.Create(this.ConversionPreset, this.intermediateFilePath);
this.compressionConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath);
this.compressionConversionJob.PrepareConversion(this.OutputFilePath);
this.compressionThread = Helpers.InstantiateThread("CDACompressionThread", this.CompressAsync);
}
@@ -28,7 +28,7 @@ namespace FileConverter.ConversionJobs
this.IsCancelable = true;
}
public ConversionJob_FFMPEG(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_FFMPEG(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
this.IsCancelable = true;
}
@@ -12,7 +12,7 @@ namespace FileConverter.ConversionJobs
private ConversionJob pngConversionJob = null;
private ConversionJob gifConversionJob = null;
public ConversionJob_Gif(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_Gif(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
this.IsCancelable = true;
}
@@ -50,7 +50,7 @@ namespace FileConverter.ConversionJobs
// Convert input in png file to send it to ffmpeg for the gif conversion.
ConversionPreset intermediatePreset = new ConversionPreset("To compatible image", OutputType.Png, this.ConversionPreset.InputTypes.ToArray());
this.pngConversionJob = ConversionJobFactory.Create(intermediatePreset, this.InputFilePath);
this.pngConversionJob.PrepareConversion(this.InputFilePath, this.intermediateFilePath);
this.pngConversionJob.PrepareConversion(this.intermediateFilePath);
inputFilePath = this.intermediateFilePath;
}
@@ -60,8 +60,8 @@ namespace FileConverter.ConversionJobs
}
// Convert png file into ico.
this.gifConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset);
this.gifConversionJob.PrepareConversion(inputFilePath, this.OutputFilePath);
this.gifConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset, inputFilePath);
this.gifConversionJob.PrepareConversion(this.OutputFilePath);
}
protected override void Convert()
@@ -11,7 +11,7 @@ namespace FileConverter.ConversionJobs
private ConversionJob pngConversionJob;
private ConversionJob icoConversionJob;
public ConversionJob_Ico(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_Ico(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
this.IsCancelable = true;
}
@@ -43,11 +43,11 @@ namespace FileConverter.ConversionJobs
intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2, "True");
intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageMaximumSize, "256");
this.pngConversionJob = ConversionJobFactory.Create(intermediatePreset, this.InputFilePath);
this.pngConversionJob.PrepareConversion(this.InputFilePath, this.intermediateFilePath);
this.pngConversionJob.PrepareConversion(this.intermediateFilePath);
// Convert png file into ico.
this.icoConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset);
this.icoConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath);
this.icoConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset, this.intermediateFilePath);
this.icoConversionJob.PrepareConversion(this.OutputFilePath);
}
protected override void Convert()
@@ -17,7 +17,7 @@ namespace FileConverter.ConversionJobs
this.IsCancelable = true;
}
public ConversionJob_ImageMagick(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_ImageMagick(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
this.IsCancelable = true;
}
@@ -21,7 +21,7 @@ namespace FileConverter.ConversionJobs
ConversionJob_Word.InitializeWordApplicationInstance();
}
public ConversionJob_Word(ConversionPreset conversionPreset) : base(conversionPreset)
public ConversionJob_Word(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
{
ConversionJob_Word.InitializeWordApplicationInstance();
}
@@ -267,6 +267,15 @@ namespace FileConverter.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Prepare conversion.
/// </summary>
public static string ConversionStatePrepareConversion {
get {
return ResourceManager.GetString("ConversionStatePrepareConversion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Read document.
/// </summary>
@@ -523,4 +523,7 @@ use maj for uppercase version</value>
<data name="ErrorConversionFailedWithOutput" xml:space="preserve">
<value>The conversion job failed but there is an output file that does exists.</value>
</data>
<data name="ConversionStatePrepareConversion" xml:space="preserve">
<value>Prepare conversion</value>
</data>
</root>
@@ -541,4 +541,7 @@ utilisez maj pour une version en majuscules</value>
<data name="ErrorConversionFailedWithOutput" xml:space="preserve">
<value>Des fichiers sont présents malgré l'echec de la conversion.</value>
</data>
<data name="ConversionStatePrepareConversion" xml:space="preserve">
<value>Préparation</value>
</data>
</root>
@@ -523,4 +523,7 @@ use maj for uppercase version</value>
<data name="ErrorConversionFailedWithOutput" xml:space="preserve">
<value>The conversion job failed but there is an output file that does exists.</value>
</data>
<data name="ConversionStatePrepareConversion" xml:space="preserve">
<value>Prepare conversion</value>
</data>
</root>