* UIFormHelper: 解决ShowNotifier打开多个,全部关闭时出错的问题

This commit is contained in:
Sunny 2022-07-17 21:09:10 +08:00
parent e3f550e6e9
commit 6b9a456172
2 changed files with 23 additions and 21 deletions

View File

@ -21,6 +21,7 @@
* 2021-05-09: V3.0.3 RemovePage接口GetTopMost为原生接口TopMost * 2021-05-09: V3.0.3 RemovePage接口GetTopMost为原生接口TopMost
* 2021-06-27: V3.0.4 FeedbackPage可将对象反馈给Frame * 2021-06-27: V3.0.4 FeedbackPage可将对象反馈给Frame
* 2021-12-13: V3.0.9 Form的ShowDialogWithMask() * 2021-12-13: V3.0.9 Form的ShowDialogWithMask()
* 2022-07-17: V3.2.1 ShowNotifier打开多个
******************************************************************************/ ******************************************************************************/
using System; using System;

View File

@ -28,9 +28,10 @@
// If you are looking for something professional, you can do it by yourself and of course share it! // If you are looking for something professional, you can do it by yourself and of course share it!
// //
using System; using System;
using System.Collections.Generic; using System.Collections.Concurrent;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Linq;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
@ -80,7 +81,7 @@ namespace Sunny.UI
} }
} }
private static readonly List<UINotifier> Notes = new List<UINotifier>(); // Keep a list of the opened Notifiers private static readonly ConcurrentDictionary<short, UINotifier> Notes = new ConcurrentDictionary<short, UINotifier>(); // Keep a list of the opened Notifiers
private NoteLocation noteLocation; // Note position private NoteLocation noteLocation; // Note position
private short ID; // Note ID private short ID; // Note ID
@ -116,11 +117,10 @@ namespace Sunny.UI
InApplication = insideMe; InApplication = insideMe;
InitializeComponent(); InitializeComponent();
if (Notes.Count == 0)
foreach (var nt in Notes) // Use the latest available ID from the note list ID = 1;
if (nt.ID > ID) else
ID = nt.ID; ID = (short)(Notes.Keys.Max() + 1); // Set the Note ID
ID++; // Set the Note ID
if (insideMe != null && !inAppNoteExists()) // Register the drag and resize events if (insideMe != null && !inAppNoteExists()) // Register the drag and resize events
{ {
@ -144,7 +144,7 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
private void inApp_LocationChanged(object sender, EventArgs e) private void inApp_LocationChanged(object sender, EventArgs e)
{ {
foreach (var note in Notes) foreach (var note in Notes.Values)
{ {
if (note.InApplication != null) if (note.InApplication != null)
{ {
@ -414,7 +414,7 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
private void closeMe() private void closeMe()
{ {
Notes.Remove(this); Notes.TryRemove(this.ID, out _);
Close(); Close();
if (Notes.Count == 0) if (Notes.Count == 0)
@ -426,11 +426,12 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
private bool inAppNoteExists() private bool inAppNoteExists()
{ {
foreach (var note in Notes) foreach (var note in Notes.Values)
{ {
if (note.InApplication != null) if (note.InApplication != null)
return true; return true;
} }
return false; return false;
} }
@ -439,12 +440,10 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
private bool isLocationAlreadyUsed(NoteLocation location, UINotifier note) private bool isLocationAlreadyUsed(NoteLocation location, UINotifier note)
{ {
foreach (var p in Notes) foreach (var p in Notes.Values)
if (p.Left == location.X && if (p.Left == location.X && p.Top == location.Y)
p.Top == location.Y)
{ {
if (note.InApplication != null && if (note.InApplication != null && p.ID == note.ID)
p.ID == note.ID)
return false; return false;
return true; return true;
} }
@ -456,10 +455,12 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
public static void CloseAll() public static void CloseAll()
{ {
for (int i = Notes.Count - 1; i >= 0; i--) foreach (var note in Notes.Values)
{ {
Notes[i].closeMe(); note.closeMe();
} }
Notes.Clear();
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
@ -518,7 +519,7 @@ namespace Sunny.UI
timer.RunWorkerAsync(not); // Timer (temporary notes) timer.RunWorkerAsync(not); // Timer (temporary notes)
} }
Notes.Add(not); // Add to our collection of Notifiers Notes.TryAdd(not.ID, not); // Add to our collection of Notifiers
updated_note_id = not.ID; updated_note_id = not.ID;
} }
@ -535,7 +536,7 @@ namespace Sunny.UI
updated_note_id = 0; updated_note_id = 0;
updated_note_occurence = 0; updated_note_occurence = 0;
foreach (var note in Notes) foreach (var note in Notes.Values)
{ {
short occurence = 0; short occurence = 0;
string filteredTitle = note.Title; string filteredTitle = note.Title;
@ -574,7 +575,7 @@ namespace Sunny.UI
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
public static void Update(short ID, string desc, UINotifierType noteType, string title) public static void Update(short ID, string desc, UINotifierType noteType, string title)
{ {
foreach (var note in Notes) foreach (var note in Notes.Values)
{ {
if (note.Tag != null && // Get the node if (note.Tag != null && // Get the node
note.Tag.Equals("__Notifier|" + ID.ToString("X4"))) note.Tag.Equals("__Notifier|" + ID.ToString("X4")))
@ -689,7 +690,7 @@ namespace Sunny.UI
break; break;
} }
Notes.Add(note); // Add to our collection of Notifiers Notes.TryAdd(note.ID, note); // Add to our collection of Notifiers
note.ShowInTaskbar = false; note.ShowInTaskbar = false;
note.ShowDialog(); note.ShowDialog();