SafeDispatch/MotoTrbo_GW/FeedbackRadMessageBox.cs

89 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Telerik.WinControls;
namespace MotoTrbo_GW
{
public class FeedbackRadMessageBox
{
// Restart if no user interaction after message box is displayed
private static Thread restartThread;
private static Boolean isMessageBoxShown = false;
public static Int16 InteractionWaitSeconds = 7;
public static void SetTheme(String themeName)
{
RadMessageBox.SetThemeName(themeName);
}
public static DialogResult ShowError(String message, String title)
{
return Show(title, message, MessageBoxButtons.OK, RadMessageIcon.Error);
}
public static DialogResult ShowInfo(String message, String title)
{
return Show(title, message, MessageBoxButtons.OK, RadMessageIcon.Info);
}
public static DialogResult ShowExclamation(String message, String title)
{
return Show(title, message, MessageBoxButtons.OK, RadMessageIcon.Exclamation);
}
public static DialogResult Show(String title, String message, MessageBoxButtons buttons, RadMessageIcon icon)
{
isMessageBoxShown = true;
restartThread = new Thread(new ThreadStart(WaitForMessageBoxInteraction));
restartThread.Start();
SafeMobileLib.Utils.WriteEventLog("Safemobile", title + ":" + message, icon == RadMessageIcon.Error ? EventLogEntryType.Error :
(icon == RadMessageIcon.Info ? EventLogEntryType.Information : EventLogEntryType.Warning), 3345);
DialogResult result = RadMessageBox.Show(message, title, buttons, icon);
isMessageBoxShown = false;
return result;
}
private static void WaitForMessageBoxInteraction()
{
try
{
int count = 0;
while (isMessageBoxShown)
{
Thread.Sleep(100);
if (count++ > InteractionWaitSeconds * 10 && isMessageBoxShown == true)
{
isMessageBoxShown = false;
SafeMobileLib.Utils.WriteEventLog("Safemobile", "Restarting MotoTRBO GW", EventLogEntryType.Information, 3345);
// restart the app if no interaction from user
OnRestartRequest?.Invoke();
}
}
}
catch (Exception ex) { }
}
public delegate void RestartRequestDel();
public static event RestartRequestDel OnRestartRequest;
}
}