Skip to content

Commit 53cf2a8

Browse files
committed
added retries to linux screenshots
1 parent b3f2c6a commit 53cf2a8

File tree

1 file changed

+60
-33
lines changed

1 file changed

+60
-33
lines changed

Services/ScreenshotService.cs

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -326,50 +326,77 @@ private void TakeLinuxScreenshot(string saveDirectory)
326326
? $"{_flameshotArgs} screen -r -p \"{savePath}\""
327327
: $"screen -r -p \"{savePath}\"";
328328

329-
ProcessStartInfo flameshotStartInfo = new()
329+
for (int attempt = 1; attempt <= 3; attempt++)
330330
{
331-
FileName = _flameshotCommand,
332-
Arguments = arguments,
333-
UseShellExecute = false,
334-
CreateNoWindow = true,
335-
};
331+
ProcessStartInfo flameshotStartInfo = new()
332+
{
333+
FileName = _flameshotCommand,
334+
Arguments = arguments,
335+
UseShellExecute = false,
336+
CreateNoWindow = true,
337+
};
336338

337-
flameshotStartInfo.EnvironmentVariables["DISPLAY"] = Environment.GetEnvironmentVariable("DISPLAY") ?? ":0";
339+
flameshotStartInfo.EnvironmentVariables["DISPLAY"] = Environment.GetEnvironmentVariable("DISPLAY") ?? ":0";
338340

339-
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("XAUTHORITY")))
340-
{
341-
flameshotStartInfo.EnvironmentVariables["XAUTHORITY"] = Environment.GetEnvironmentVariable("XAUTHORITY");
342-
}
343-
344-
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WAYLAND_DISPLAY")))
345-
{
346-
flameshotStartInfo.EnvironmentVariables["WAYLAND_DISPLAY"] = Environment.GetEnvironmentVariable("WAYLAND_DISPLAY");
347-
}
341+
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("XAUTHORITY")))
342+
{
343+
flameshotStartInfo.EnvironmentVariables["XAUTHORITY"] = Environment.GetEnvironmentVariable("XAUTHORITY");
344+
}
348345

349-
Process process = Process.Start(flameshotStartInfo);
346+
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WAYLAND_DISPLAY")))
347+
{
348+
flameshotStartInfo.EnvironmentVariables["WAYLAND_DISPLAY"] = Environment.GetEnvironmentVariable("WAYLAND_DISPLAY");
349+
}
350350

351-
if (process.WaitForExit(3000))
352-
{
353-
process.Close();
351+
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR")))
352+
{
353+
flameshotStartInfo.EnvironmentVariables["XDG_RUNTIME_DIR"] = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
354+
}
354355

355-
Thread.Sleep(100);
356+
int? exitCode = null;
357+
Process process = Process.Start(flameshotStartInfo);
356358

357-
if (!File.Exists(savePath))
359+
try
358360
{
359-
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - Screenshot may have failed");
360-
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - {_flameshotCommand} {arguments}");
361-
}
362-
}
363-
else
364-
{
365-
process.Kill();
366-
process.Close();
361+
if (process.WaitForExit(3000))
362+
{
363+
exitCode = process.ExitCode;
364+
}
365+
else
366+
{
367+
process.Kill();
368+
}
367369

368-
Thread.Sleep(100);
370+
Thread.Sleep(100);
369371

370-
if (!File.Exists(savePath))
372+
if (!File.Exists(savePath))
373+
{
374+
if (attempt == 3)
375+
{
376+
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - {_flameshotCommand} {arguments}");
377+
378+
if (exitCode != null)
379+
{
380+
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - Screenshot may have failed ({exitCode})");
381+
}
382+
else
383+
{
384+
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - Flameshot process timed out: killed and screenshot not found");
385+
}
386+
}
387+
else
388+
{
389+
Thread.Sleep(250);
390+
}
391+
}
392+
else
393+
{
394+
break;
395+
}
396+
}
397+
finally
371398
{
372-
_loggingService.LogInfo($"{nameof(ScreenshotService)}>{nameof(TakeLinuxScreenshot)} - Flameshot process timed out, killed and screenshot not found.");
399+
process.Close();
373400
}
374401
}
375402
}

0 commit comments

Comments
 (0)