Skip to content

Commit

Permalink
より堅牢に一時ファイルをクリーンアップ
Browse files Browse the repository at this point in the history
  • Loading branch information
yutokun committed Nov 22, 2020
1 parent dc7e0da commit a32788e
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions APKInstaller/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,44 @@ public static void Cleanup(object sender, CancelEventArgs cancelEventArgs)
{
if (OtherInstanceExists()) return;

Message.Add("クリーンアップ中...");

while (IsLocked(TempDirectory))
{
Thread.Sleep(500);
}

Directory.Delete(TempDirectory, true);
Message.Add("完了");
Thread.Sleep(1000);
var directoryIsEmpty = true;
var files = Directory.GetFiles(TempDirectory);
foreach (var file in files)
{
var tryout = 0;
while (IsLocked(file) && tryout < 5)
{
++tryout;
Thread.Sleep(200);
}

if (tryout >= 5)
{
directoryIsEmpty = false;
continue;
}

File.Delete(file);
}

if (directoryIsEmpty)
{
Directory.Delete(TempDirectory, true);
Message.Add("完了");
Thread.Sleep(1000);
}
else
{
Message.Add("他のアプリケーションが通信機能を使用しているため、クリーンアップをスキップしました。");
Thread.Sleep(4000);
}
}

public static bool OtherInstanceExists()
Expand Down

0 comments on commit a32788e

Please sign in to comment.