Use delegates & events

This commit is contained in:
mashed-potatoes 2020-06-28 19:05:03 +05:00
parent ab65086cfe
commit 61bbfa98e5
4 changed files with 9 additions and 22 deletions

View file

@ -58,7 +58,7 @@ namespace PotatoNV_next.Controls
#if DEBUG
Log.PrintDebug = true;
#endif
Log.AttachListener(AppendLine);
Log.Notify += AppendLine;
}
}
}

View file

@ -28,7 +28,7 @@ namespace PotatoNV_next.Controls
InitializeComponent();
usbController = new UsbController();
usbController.AddListener(HandleDevices);
usbController.Notify += HandleDevices;
usbController.StartWorker();
}

View file

@ -10,7 +10,8 @@ namespace PotatoNV_next.Utils
{
public static bool PrintDebug { get; set; } = false;
private static List<Action<LogEventArgs>> actions = new List<Action<LogEventArgs>>();
public delegate void LogHandler(LogEventArgs logEventArgs);
public static event LogHandler Notify;
public enum Status
{
@ -44,10 +45,7 @@ namespace PotatoNV_next.Utils
Message = message + Environment.NewLine
};
foreach (var act in actions)
{
act?.Invoke(args);
}
Notify?.Invoke(args);
}
public static void Debug(string message)
@ -69,11 +67,6 @@ namespace PotatoNV_next.Utils
{
AppendToLog(Status.Error, message);
}
public static void AttachListener(Action<LogEventArgs> act)
{
actions.Add(act);
}
}
public class LogEventArgs : EventArgs

View file

@ -111,11 +111,13 @@ namespace PotatoNV_next.Utils
#endregion
#region Caller
private List<Action<Device[]>> listeners = new List<Action<Device[]>>();
public delegate void DevicesHandler(Device[] devices);
public event DevicesHandler Notify;
private void UpdateList()
{
var list = new List<Device>();
try
{
list.AddRange(GetDownloadVCOMDevices());
@ -136,15 +138,7 @@ namespace PotatoNV_next.Utils
cachedDevices = sns;
foreach (var listener in listeners)
{
listener?.Invoke(list.ToArray());
}
}
public void AddListener(Action<Device[]> action)
{
listeners.Add(action);
Notify?.Invoke(list.ToArray());
}
#endregion
}