問題:インストールプロセス中に、ファイルとフォルダーが作成、移動、および名前変更されます。インストール中にサービスも開始および停止するため、1つ以上のファイルまたはフォルダーでロックの問題が発生する可能性があります。ほとんどのロックシナリオを回避するための解決策がありますが、時間やCPUを浪費せず、ユーザーが[中止/再試行/無視]ダイアログをできるだけ処理する必要がない、より良いアプローチを望んでいます。
背景:最適なシナリオでは、サービスの開始と停止を処理するロジック、およびファイルとフォルダーにロックを設定するその他の操作により、インストーラーの作成/移動/名前変更セクションが発生するまでにロックがまだ存在しないようにします。この場合、インストールプロセスのこれらの部分を制御することはできませんが、ファイルまたはフォルダーを変更できない場合は、ユーザーエラーダイアログをできるだけ表示しないようにする必要があります。
サードパーティのアプリケーションが必要なリソースをロックしているという正当なエラーケースがあるため、長期間単純にループすることはできません。この情報をユーザーに迅速に表示し、問題のあるプロセスに関する情報を表示できるようにする必要があります。 。ただし、私のユースケースには多くの誤検知があり、ユーザーの介入を最小限に抑えたいと考えています。
私の現在の解決策は次のようになります。
private const int MaxRetry = 3;
private const int RetryDelayMs = 250;
private static bool DoAction(FileSystemAction action, String src, String dest = "")
{
bool retval = false;
bool inprogress = true;
// (Re)Attempt to perform the desired action until the user aborts.
while (inprogress)
{
try
{
// Automatically retry N times with a delay of M milliseconds.
// This is meant to reduce the number of times a user sees an error dialog.
var autoretry = true;
var retryCounter = 0;
while (autoretry)
{
try
{
// CODE: Attempt to perform the file system action.
// Only get here if we succeeded.
autoretry = false;
}
catch (Exception)
{
// If this operation failed, try a few times.
if (retryCounter >= MaxRetry)
throw;
retryCounter++;
Thread.Sleep(RetryDelayMs);
}
}
// If we get here without exception, success!
inprogress = false;
retval = true;
}
catch (Exception ex)
{
// Since there was an error, we now do the following:
// - Determine if locked files are the cause.
// - If locked files are the cause:
// - Show a custom Locked File dialog, providing information.
// - Otherwise:
// - Select message string based on action to inform the user.
// - Allow the user to Retry/Abort/Ignore the failed operation.
// CODE: Logic goes here.
}
}
return retval;
}
このロジックを最初に作成したとき、ここでロックされたファイルと失敗したファイルシステム操作を処理するいくつかの例を参照しました。ただし、このソリューションは、すべての誤検知の状況(競合状態がある場合)でユーザーがダイアログを表示することを妨げないため、このソリューションには満足していません。問題のあるサービスロジックを書き直すことができずにこれを改善するためのアイデアはありますか?