mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-21 10:01:58 +01:00
Close office applications when conversion fail.
This commit is contained in:
1 parent
f57e9ce7ac
commit
962f29d35a
4 files changed
+78
-70
No files matched your search
@@ -100,7 +100,7 @@ namespace FileConverter.ConversionJobs
|
||||
this.document.Close(false);
|
||||
this.document = null;
|
||||
|
||||
this.ReleaseApplicationInstanceIfNeeded();
|
||||
this.ReleaseOfficeApplicationInstanceIfNeeded();
|
||||
|
||||
if (this.pdf2ImageConversionJob != null)
|
||||
{
|
||||
@@ -133,6 +133,27 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitializeOfficeApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.application != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize excel application.
|
||||
Diagnostics.Debug.Log("Instantiate excel application via interop.");
|
||||
this.application = new Excel.Application
|
||||
{
|
||||
Visible = false
|
||||
};
|
||||
}
|
||||
|
||||
protected override void ReleaseOfficeApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit excel application via interop.");
|
||||
this.application.Quit();
|
||||
}
|
||||
|
||||
private async Task UpdateProgress()
|
||||
{
|
||||
while (this.pdf2ImageConversionJob.State != ConversionState.Done &&
|
||||
@@ -155,7 +176,7 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
private void LoadDocumentIfNecessary()
|
||||
{
|
||||
this.InitializeApplicationInstanceIfNecessary();
|
||||
this.InitializeOfficeApplicationInstanceIfNecessary();
|
||||
|
||||
if (this.document == null)
|
||||
{
|
||||
@@ -164,26 +185,5 @@ namespace FileConverter.ConversionJobs
|
||||
this.document = this.application.Workbooks.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.application != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize excel application.
|
||||
Diagnostics.Debug.Log("Instantiate excel application via interop.");
|
||||
this.application = new Excel.Application
|
||||
{
|
||||
Visible = false
|
||||
};
|
||||
}
|
||||
|
||||
private void ReleaseApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit excel application via interop.");
|
||||
this.application.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,16 @@ namespace FileConverter.ConversionJobs
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnConversionFailed()
|
||||
{
|
||||
base.OnConversionFailed();
|
||||
|
||||
this.ReleaseOfficeApplicationInstanceIfNeeded();
|
||||
}
|
||||
|
||||
protected abstract void InitializeOfficeApplicationInstanceIfNecessary();
|
||||
|
||||
protected abstract void ReleaseOfficeApplicationInstanceIfNeeded();
|
||||
}
|
||||
}
|
||||
@@ -80,9 +80,6 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
this.LoadDocumentIfNecessary();
|
||||
|
||||
// Make this document the active document.
|
||||
//this.document.Activate();
|
||||
|
||||
this.UserState = Properties.Resources.ConversionStateConversion;
|
||||
|
||||
Diagnostics.Debug.Log("Convert PowerPoint document to pdf.");
|
||||
@@ -92,7 +89,7 @@ namespace FileConverter.ConversionJobs
|
||||
this.document.Close();
|
||||
this.document = null;
|
||||
|
||||
this.ReleaseApplicationInstanceIfNeeded();
|
||||
this.ReleaseOfficeApplicationInstanceIfNeeded();
|
||||
|
||||
if (this.pdf2ImageConversionJob != null)
|
||||
{
|
||||
@@ -125,6 +122,24 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitializeOfficeApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.application != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize PowerPoint application.
|
||||
Diagnostics.Debug.Log("Instantiate PowerPoint application via interop.");
|
||||
this.application = new PowerPoint.Application();
|
||||
}
|
||||
|
||||
protected override void ReleaseOfficeApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit PowerPoint application via interop.");
|
||||
this.application.Quit();
|
||||
}
|
||||
|
||||
private async Task UpdateProgress()
|
||||
{
|
||||
while (this.pdf2ImageConversionJob.State != ConversionState.Done &&
|
||||
@@ -147,32 +162,14 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
private void LoadDocumentIfNecessary()
|
||||
{
|
||||
this.InitializeApplicationInstanceIfNecessary();
|
||||
this.InitializeOfficeApplicationInstanceIfNecessary();
|
||||
|
||||
if (this.document == null)
|
||||
{
|
||||
Diagnostics.Debug.Log("Load PowerPoint document '{0}'.", this.InputFilePath);
|
||||
|
||||
this.document = this.application.Presentations.Open(this.InputFilePath, ReadOnly:MsoTriState.msoTrue, WithWindow:MsoTriState.msoFalse);
|
||||
this.document = this.application.Presentations.Open(this.InputFilePath, ReadOnly: MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.application != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize PowerPoint application.
|
||||
Diagnostics.Debug.Log("Instantiate PowerPoint application via interop.");
|
||||
this.application = new PowerPoint.Application();
|
||||
}
|
||||
|
||||
private void ReleaseApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit PowerPoint application via interop.");
|
||||
this.application.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ namespace FileConverter.ConversionJobs
|
||||
this.document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
|
||||
this.document = null;
|
||||
|
||||
this.ReleaseWordApplicationInstanceIfNeeded();
|
||||
this.ReleaseOfficeApplicationInstanceIfNeeded();
|
||||
|
||||
if (this.pdf2ImageConversionJob != null)
|
||||
{
|
||||
@@ -125,6 +125,27 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitializeOfficeApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.wordApplication != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize word application.
|
||||
Diagnostics.Debug.Log("Instantiate word application via interop.");
|
||||
this.wordApplication = new Microsoft.Office.Interop.Word.Application
|
||||
{
|
||||
Visible = false
|
||||
};
|
||||
}
|
||||
|
||||
protected override void ReleaseOfficeApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit word application via interop.");
|
||||
this.wordApplication.Quit();
|
||||
}
|
||||
|
||||
private async Task UpdateProgress()
|
||||
{
|
||||
while (this.pdf2ImageConversionJob.State != ConversionState.Done &&
|
||||
@@ -147,7 +168,7 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
private void LoadDocumentIfNecessary()
|
||||
{
|
||||
this.InitializeWordApplicationInstanceIfNecessary();
|
||||
this.InitializeOfficeApplicationInstanceIfNecessary();
|
||||
|
||||
if (this.document == null)
|
||||
{
|
||||
@@ -156,26 +177,5 @@ namespace FileConverter.ConversionJobs
|
||||
this.document = this.wordApplication.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeWordApplicationInstanceIfNecessary()
|
||||
{
|
||||
if (this.wordApplication != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize word application.
|
||||
Diagnostics.Debug.Log("Instantiate word application via interop.");
|
||||
this.wordApplication = new Microsoft.Office.Interop.Word.Application
|
||||
{
|
||||
Visible = false
|
||||
};
|
||||
}
|
||||
|
||||
private void ReleaseWordApplicationInstanceIfNeeded()
|
||||
{
|
||||
Diagnostics.Debug.Log("Quit word application via interop.");
|
||||
this.wordApplication.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user