mirror of
https://github.com/mashed-potatoes/PotatoNV.git
synced 2024-09-20 04:54:00 +02:00
Use delegates & events
This commit is contained in:
parent
ab65086cfe
commit
61bbfa98e5
4 changed files with 9 additions and 22 deletions
|
@ -58,7 +58,7 @@ namespace PotatoNV_next.Controls
|
|||
#if DEBUG
|
||||
Log.PrintDebug = true;
|
||||
#endif
|
||||
Log.AttachListener(AppendLine);
|
||||
Log.Notify += AppendLine;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace PotatoNV_next.Controls
|
|||
InitializeComponent();
|
||||
|
||||
usbController = new UsbController();
|
||||
usbController.AddListener(HandleDevices);
|
||||
usbController.Notify += HandleDevices;
|
||||
usbController.StartWorker();
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue