Commit 75aec1ee authored by tsuji's avatar tsuji

検版実行中のキャンセル処理を追加

parent 6c87f8dd
......@@ -146,7 +146,8 @@ namespace CSRender
//指定先にレポートを移動
internal static void moveReport(string resultPath, string workPath)
{
string reportPath = System.IO.Path.Combine(resultPath, "report", "Log");
//string reportPath = System.IO.Path.Combine(resultPath, "report", "Log");
string reportPath = System.IO.Path.Combine(resultPath, "Log");
if (!Directory.Exists(reportPath))//指定先にreportフォルダがあるか
{
Directory.CreateDirectory(reportPath);//なければ新規でreportフォルダを作成
......
......@@ -338,5 +338,25 @@ namespace CSRender
Directory.CreateDirectory(inDirPath);//同名のディレクトリを作成
}
//キャンセルが実行されたときに、途中で生成したファイル群を削除
private void DelDirCancel(string inWorkPath)
{
try
{
if (Directory.Exists(System.IO.Path.Combine(inWorkPath, "OUT_DIFF")))
{
Directory.Delete(System.IO.Path.Combine(inWorkPath, "OUT_DIFF"), true);//OUT_DIFFを削除
}
if (Directory.Exists(System.IO.Path.Combine(inWorkPath, "work")))
{
Directory.Delete(System.IO.Path.Combine(inWorkPath, "work"), true);//作業(work)ディレクトリを削除
}
}
catch(Exception ex)
{
return;
}
}
}
}
......@@ -62,9 +62,9 @@
</ComboBox.Background>
</ComboBox>
</StackPanel>
<Label Content="出力形式:" Margin="41,0,11,1.667" VerticalAlignment="Bottom" RenderTransformOrigin="0.544,1.949" FontSize="14" Grid.Row="9" Height="29" Grid.RowSpan="2"/>
<Label Content="出力形式:" Margin="9,0,43,2" VerticalAlignment="Bottom" RenderTransformOrigin="0.544,1.949" FontSize="14" Grid.Row="9" Height="29" Grid.RowSpan="2"/>
<Label x:Name="pageLabel" Content="指定ページ:" Margin="1,27.667,91.667,0" VerticalAlignment="Top" RenderTransformOrigin="0.544,1.949" FontSize="14" Grid.Row="5" Height="29" IsEnabled="{Binding PageRange_SelectionChanged}" Grid.RowSpan="3" Grid.Column="1"/>
<StackPanel HorizontalAlignment="Left" Margin="119,2.333,0,0.333" Grid.Row="10" Width="71" Grid.RowSpan="2" Grid.ColumnSpan="2" >
<StackPanel HorizontalAlignment="Left" Margin="84,2,0,0" Grid.Row="10" Width="71" Grid.RowSpan="2" Grid.ColumnSpan="2" >
<ComboBox x:Name="FileSelect" SelectedValuePath="Id" DisplayMemberPath="Item" Height="21" SelectedValue="1" Margin="0,0,9.667,0" BorderBrush="#FF3A3A3A" >
<ComboBox.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
......@@ -78,9 +78,10 @@
TextWrapping="Wrap" Text="1,2,4-6" VerticalAlignment="Top" FontSize="14" IsEnabled="{Binding PageRange_SelectionChanged}" Grid.ColumnSpan="3"
InputMethod.IsInputMethodSuspended="True" PreviewKeyDown="PageBox_PreviewKeyDown" PreviewTextInput="PageBox_PreviewTextInput" LostFocus="PageBox_LostFocus" CommandManager.PreviewExecuted="PageBox_PreviewExecuted" BorderBrush="#FF595959"/>
<TextBox x:Name="ResultConsole" Margin="44,6.667,44.333,8.667" Grid.Row="9" Grid.ColumnSpan="4" VerticalScrollBarVisibility="Auto" FontSize="14" IsReadOnly="True" HorizontalScrollBarVisibility="Auto" BorderBrush="#FF595959" Panel.ZIndex="1"/>
<Button x:Name="TIFFButton" Content="画像出力" HorizontalAlignment="Left" Height="22" Margin="52,2.333,0,0" Grid.Row="10" VerticalAlignment="Top" Width="82" Click="TIFFButtonClick" Grid.Column="1"/>
<Button x:Name="CompareButton" Content="比較" HorizontalAlignment="Left" Height="22" Margin="143,2.333,0,0" Grid.Row="10" VerticalAlignment="Top" Width="82" Grid.Column="1" Click="CompareButtonClick" Grid.ColumnSpan="2"/>
<Button x:Name="closeButton" Content="Close" Height="22" Margin="0,2.333,45.333,0" Grid.Row="10" VerticalAlignment="Top" Grid.Column="2" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="77" Click="CloseButton_Click"/>
<Button x:Name="TIFFButton" Content="画像出力" HorizontalAlignment="Left" Height="22" Margin="17,2,0,0" Grid.Row="10" VerticalAlignment="Top" Width="70" Click="TIFFButtonClick" Grid.Column="1"/>
<Button x:Name="CompareButton" Content="比較" HorizontalAlignment="Left" Height="22" Margin="96,2,0,0" Grid.Row="10" VerticalAlignment="Top" Width="70" Grid.Column="1" Click="CompareButtonClick"/>
<Button x:Name="closeButton" Content="Close" Height="22" Margin="0,2.333,45.333,0" Grid.Row="10" VerticalAlignment="Top" Grid.Column="2" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="70" Click="CloseButton_Click"/>
<Button x:Name="cancelButton" Content="Cancel" Height="22" Margin="0,2,39,0" Grid.Row="10" VerticalAlignment="Top" Grid.Column="2" HorizontalAlignment="Right" Width="70" Click="CancelButton_Click"/>
<Label Content="位置ズレ許容量(0 - 2):" Margin="1,0.667,29.667,0" VerticalAlignment="Top" RenderTransformOrigin="0.544,1.949" FontSize="14" Grid.Row="7" Height="29" Grid.RowSpan="2" Grid.Column="1"/>
<StackPanel Grid.Column="1" Grid.Row="7" Orientation="Horizontal" HorizontalAlignment="Left" Margin="153,4.667,0,2.333" Width="32" Background="WhiteSmoke">
......
......@@ -18,6 +18,7 @@ using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Threading;
using System.Threading;
namespace CSRender
{
......@@ -80,6 +81,25 @@ namespace CSRender
private void CloseButton_Click(object sender, RoutedEventArgs e) =>
Close();
private CancellationTokenSource CancelToken = null;
//private CancellationToken Canceller;
//Cancelボタン実行
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
isCanceled = true;
ResultConsole.Text += " 処理を中断します\r\n";
if (CancelToken == null) { return; }
else { CancelToken.Cancel(); }
//if (CancelToken?.IsCancellationRequested == false)
//{ CancelToken.Cancel(); }
}
/// <summary>
/// キャンセルフラグ
/// </summary>
private bool isCanceled = false;
/// <summary>
/// 比較ボタンを実行する。
/// </summary>
......@@ -107,6 +127,17 @@ namespace CSRender
{
killButton();//メニューを非活性化
isCanceled = false;//Cancelフラグを初期化
CancelToken = new CancellationTokenSource();
var Canceller = CancelToken.Token;
//前回のOUT_DIFFフォルダを削除
string OldOutDiff = System.IO.Path.Combine(Properties.Settings.Default.workSpaceSetting, "OUT_DIFF");
if (Directory.Exists(OldOutDiff))
{
Directory.Delete(OldOutDiff, true);
}
//各種パスの指定先の取得
string workBoxText = workBox.Text;
string reportBoxText = reportBox.Text;
......@@ -186,7 +217,7 @@ namespace CSRender
//前回のログが内部に残っていれば削除(エラー防止)
CompareTIFF.deleteOldLog(System.IO.Path.Combine(workDir, "report"));
string workLogPath = System.IO.Path.Combine(workBoxText, "report");//reportフォルダのパス
string workLogPath = System.IO.Path.Combine(reportBoxText, "Log");//reportフォルダのパス
checkDirPath(workLogPath);//なければ作成
string outDiffPath = System.IO.Path.Combine(workBoxText, "OUT_DIFF");//DIFF画像の出力先パス
checkDirPath(outDiffPath);//なければ作成
......@@ -195,6 +226,10 @@ namespace CSRender
{
//検版レポートの入力チェック
reportFlag = checkReportPath();
if (reportBoxText == "")
{
reportBoxText = reportBox.Text;
}
}
catch (Exception ex)
{
......@@ -213,6 +248,9 @@ namespace CSRender
selectedPage = pageBox.Text;
}
//実処理スタート
try
{
//TIFF画像の生成
ResultConsole.Text += "< TIFF変換開始 >\r\n";
ResultConsole.Text += " 対象ファイルをTIFF画像に変換中\r\n";
......@@ -223,14 +261,38 @@ namespace CSRender
string targetTIFFPath = System.IO.Path.Combine(workBoxText, "work", "targetTIFF");
DeleteOldDir(targetTIFFPath); //指定先に古いTIFF用フォルダがあれば削除し、新たに作成(前回作業時の生成物との混同防止)
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DoEvents();
DelDirCancel(workBoxText);
reviveButton();
return;
}
//CSRenderの実行
try
{
await Task.Run(() =>
//await Task.Factory.StartNew(() =>
{
string targetTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, targetBoxText, targetTIFFPath);
});
//string targetTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, targetBoxText, targetTIFFPath, Canceller, isCanceled);
}, Canceller);
//string targetTIFFResult = await OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, targetBoxText, targetTIFFPath, Canceller, isCanceled);
}
catch (OperationCanceledException)
{
CancelToken.Dispose();
CancelToken = null;
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
DoEvents();
return;
}
catch (Exception ex)
{
......@@ -238,22 +300,46 @@ namespace CSRender
ExceptionMessage(ex, workLogPath, WorkCategory);
}
if (!isCanceled)
{
ResultConsole.Text += " リファレンスファイルをTIFF画像に変換中\r\n";
ResultConsole.ScrollToEnd();
DoEvents();
}
//リファレンスフォルダ
string referenceTIFFPath = System.IO.Path.Combine(workBoxText, "work", "referenceTIFF");
DeleteOldDir(referenceTIFFPath); //指定先に古いTIFF用フォルダがあれば削除し、新たに作成(前回作業時の生成物との混同防止)
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
DoEvents();
reviveButton();
return;
}
//CSRenderの実行
try
{
await Task.Run(() =>
{
string referenceTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, referenceBoxText, referenceTIFFPath);
});
//string referenceTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, referenceBoxText, referenceTIFFPath, Canceller, isCanceled);
}, Canceller);
//string referenceTIFFResult = await OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, referenceBoxText, referenceTIFFPath, Canceller, isCanceled);
}
catch (OperationCanceledException)
{
CancelToken.Dispose();
CancelToken = null;
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
DoEvents();
return;
}
catch (Exception ex)
{
......@@ -261,15 +347,33 @@ namespace CSRender
ExceptionMessage(ex, workLogPath, WorkCategory);
}
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
return;
}
ripTime.Stop();//RIP時間の計測終了
TimeSpan timeResult = ripTime.Elapsed;
var allRipTime = $"{timeResult}".Substring(0,8);
var allRipTime = $"{timeResult}".Substring(0, 8);
ResultConsole.Text += " [RIP処理時間 " + allRipTime + "]\r\n";
ResultConsole.Text += "< TIFF変換完了 >\r\n";
ResultConsole.ScrollToEnd();
DoEvents();
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
return;
}
try
{
//レポートへの出力内容を編集
......@@ -299,6 +403,7 @@ namespace CSRender
// 並列処理オプションの設定
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 4;// 並行数(プロセス数)
//parallelOptions.CancellationToken = CancelToken.Token;
var logicalProcessNum = Environment.ProcessorCount;//PCの論理CPUの数
if (logicalProcessNum >= 10)
{
......@@ -310,12 +415,33 @@ namespace CSRender
shiftPixel, colorMargin, removePoint, shadingOff, appDirectory);
await Task.Factory.StartNew(() => {
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理を中断します\r\n";
CancelToken.Cancel();
DoEvents();
reviveButton();
return;
}
Parallel.ForEach(inspectionTargets, parallelOptions, targetPDFName =>
{
//キャンセルフラグONなら終了
if (isCanceled)
{
return;
}
//PureVerifyの実行
int[] NGTiffNum = CompareTIFF.runPureVerify(targetTIFFPath, referenceTIFFPath,
shiftPixel, colorMargin, removePoint, shadingOff, reportFlag, reportBoxText,
outDiffPath, targetPDFName, NowTime, reportContentsInfo, appDirectory, exePath, verifyArgs);
//キャンセルフラグONなら終了
if (isCanceled)
{
return;
}
ResultConsole.Dispatcher.Invoke(new Action(() =>
{
outProgressToUI(tiffString, NGTiffNum, allPDFNum);
......@@ -325,6 +451,16 @@ namespace CSRender
});
}
catch (OperationCanceledException)
{
CancelToken.Dispose();
CancelToken = null;
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
DoEvents();
return;
}
catch (Exception ex)
{
string WorkCategory = "検版処理";
......@@ -336,6 +472,18 @@ namespace CSRender
//レポートを出力
//outputLog(System.IO.Path.Combine(workDir, "report"));
NGPDFNum = CompareTIFF.getNGNum(System.IO.Path.Combine(workDir, "report"));
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
DoEvents();
reviveButton();
return;
}
//UIの更新
ResultConsole.Text = tiffString;
outResultToUI(allPDFNum, NGPDFNum, referenceTIFFPath, targetTIFFPath);
}
......@@ -348,8 +496,18 @@ namespace CSRender
string PDFName = "";
try
{
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
reviveButton();
return;
}
//レポート名を取得
if (reportBox.Text != ""){ PDFName = reportBoxText; }
if (reportBox.Text != "") { PDFName = reportBoxText; }
else { PDFName = CompareTIFF.getReportName(workDir); }
}
catch (Exception ex)
......@@ -360,8 +518,19 @@ namespace CSRender
try
{
if (isCanceled)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
DoEvents();
reviveButton();
return;
}
//指定先にレポートを移動
CompareTIFF.moveReport(workBoxText, workDir);
CompareTIFF.moveReport(reportBoxText, workDir);
}
catch (Exception ex)
{
......@@ -371,9 +540,9 @@ namespace CSRender
//レポートのパスを表示
ResultConsole.Text += " 検版レポートのパス:";
ResultConsole.Text += System.IO.Path.Combine(workBoxText, "report", PDFName);
ResultConsole.Text += System.IO.Path.Combine(workBoxText, PDFName);
ResultConsole.Text += "\r\n";
if(reportBox.Text == "")
if (reportBox.Text == "")
{
reportBox.Text = System.IO.Path.Combine(workBoxText, "report", PDFName);
}
......@@ -410,6 +579,18 @@ namespace CSRender
reviveButton();
}
catch (OperationCanceledException)
{
// キャンセルされたら終了
ResultConsole.Text += " 処理が中断されました\r\n";
DelDirCancel(workBoxText);
CancelToken.Dispose();
CancelToken = null;
DoEvents();
reviveButton();
return;
}
}
/// <summary>
/// UIに検版結果を出力する。
......@@ -591,6 +772,16 @@ namespace CSRender
ResultConsole.ScrollToEnd();
}
/// <summary>
/// キャンセル実行時に生成済みのファイルを削除
/// </summary>
private void canceledFile(string inWorkPath, string inReportPath)
{
}
/// <summary>
/// TIFF画像化を実行する
/// </summary>
......@@ -598,8 +789,13 @@ namespace CSRender
/// <param name="selectedPage">入力されたページ数</param>
/// <param name="TIFFresult">CSRenderの処理出力文</param>
///
private void TIFFButtonClick(object sender, RoutedEventArgs e)
private async void TIFFButtonClick(object sender, RoutedEventArgs e)
{
isCanceled = false;//Cancelフラグを初期化
//キャンセルトークン生成
//CancellationTokenSource CancelToken = new CancellationTokenSource();
var Canceller = CancelToken.Token;
//出力メニューにメッセージを出力
ResultConsole.Text += "<変換開始>\r\n";
ResultConsole.Text += " PDFを" + FileSelect.Text + "に変換します\r\n";
......@@ -648,10 +844,10 @@ namespace CSRender
}
//CSRenderの実行
string TIFFresult =
OutputTIFF.runCSRender(ResolutionBox.Text, FileSelect.Text, pageFlag, selectedPage, targetBox.Text, workBox.Text);
//string TIFFresult =
OutputTIFF.runCSRender(ResolutionBox.Text, FileSelect.Text, pageFlag, selectedPage, targetBox.Text, workBox.Text, Canceller, isCanceled);
ResultConsole.Text += TIFFresult;
//ResultConsole.Text += TIFFresult;
ResultConsole.Text += "\r\n<変換終了>\r\n";
ResultConsole.ScrollToEnd();
......@@ -742,5 +938,6 @@ namespace CSRender
VisualBox.Enabled = true;
pageBox.IsEnabled = true;
}
}
}
......@@ -16,13 +16,18 @@ using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace CSRender
{
class OutputTIFF
partial class OutputTIFF
{
public static string runCSRender(string resolutionNum, string outputFile,
bool pageFlag, string filePage, string targetPath, string workPath)
public static void runCSRender(string resolutionNum, string outputFile,
bool pageFlag, string filePage, string targetPath, string workPath, CancellationToken inCanceller, bool inCanFlag)
{
//string resuletString = "";
try
{
//実行用コンソール呼び出し
ProcessStartInfo processStartInfo = new ProcessStartInfo();
......@@ -63,11 +68,77 @@ namespace CSRender
//CSRender.exeを実行
Process process = Process.Start(processStartInfo);
string resuletString = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Boolean ProcEndFlag = false;
if (inCanceller.IsCancellationRequested)
{
process.Kill();
inCanceller.ThrowIfCancellationRequested();
//process.Close();
// if (Directory.Exists(System.IO.Path.Combine(inWorkPath, "OUT_DIFF")))
// {
// Directory.Delete(System.IO.Path.Combine(inWorkPath, "OUT_DIFF"), true);//OUT_DIFFを削除
// }
// if (Directory.Exists(System.IO.Path.Combine(inWorkPath, "work")))
// {
// Directory.Delete(System.IO.Path.Combine(inWorkPath, "work"), true);//作業(work)ディレクトリを削除
// }
return;
}
////string resuletString = process.StandardOutput.ReadToEnd();
//resuletString = process.StandardOutput.ReadToEnd();
//resuletString = await ReadLineAsync(process, resuletString);
//process.WaitForExit();
while (!ProcEndFlag)
{
if (inCanceller.IsCancellationRequested)
{
process.Kill();
inCanceller.ThrowIfCancellationRequested();
return;
}
ProcEndFlag = process.WaitForExit(10000);
}
process.Close();
}
catch (OperationCanceledException)
{
return;
}
return resuletString;
return;
}
private async static Task<string> ReadLineAsync(Process inProc, string inLineStr)
{
inLineStr = await inProc.StandardOutput.ReadToEndAsync();
return inLineStr;
}
//public static async Task<string>runExe(string resolutionNum, string outputFile,
// bool pageFlag, string filePage, string targetPath, string workPath, CancellationToken inCanceller)
//{
// string resuletString = "";
// try
// {
// await Task.Run(async () =>
// //await Task.Factory.StartNew(() =>
// {
// resuletString =
// OutputTIFF.runCSRender(resolutionNum, outputFile, pageFlag, filePage, targetPath, workPath, inCanceller);
// }, inCanceller);
// }
// catch(OperationCanceledException)
// {
// return "Canceled";
// }
// return resuletString;
//}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment