如果有任何消息(代码如下),我有一个窗口会提醒用户。
每次timer1.Elapsed
,应用程序的内存都会增加超过 1 MB。在 10 次警报时间后,它需要超过 15 MB。然后如果有任何警报,使用的内存几乎不会上升。
我认为一个简单窗口的 15 MB 内存相当大,也许我的应用程序存在内存泄漏。但我是新手,不知道如何解决。你能帮我看看我的代码有什么问题导致内存泄漏吗?非常感谢!
XAML:
<TextBlock TextWrapping="Wrap"
FontSize="11"
Padding="5,3,3,3">
<Run x:Name="a_time"
Foreground="Blue" />
<Run x:Name="a_now"
FontStyle="Italic" />
<Run x:Name="a_id" />
<LineBreak />
<Run x:Name="a_name"
Foreground="Blue" />
<Run x:Name="a_title"
Foreground="Red"
Tag=""
MouseUp="a_MouseUp"
Cursor="Hand" />
<LineBreak />
<Run x:Name="a_content"
Foreground="Gray" />
</TextBlock>
代码:
List<Alert> alert_list = new List<Alert>();
void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (alert_list.Count == 0) return;
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
this.Background = (this.Background == Brushes.Yellow) ? Brushes.LemonChiffon : Brushes.Yellow;
Point m = Mouse.GetPosition(this);
if ((m.X > 0 & m.X < Width & m.Y > 0 & m.Y < Height) | Keyboard.IsKeyDown(Key.LeftCtrl) | Keyboard.IsKeyDown(Key.RightCtrl)) { showemail_tick = 1; return; }
Alert al = alert_list[alert_list.Count - 1];
a_title.Tag = al.e_link;
a_time.Text = al.e_time + " ";
a_now.Text = DateTime.Now.ToString("dd/MM/yy HH:mm");
a_id.Text = al.e_id;
a_name.Text = al.e_name + " ";
a_title.Text = al.e_title;
a_content.Text = al.e_content;
alert_list.RemoveAt(alert_list.Count - 1);
}));
}
public class Alert
{
public string e_time = "";
public string e_id = "";
public string e_link = "";
public string e_name = "";
public string e_title = "";
public string e_content = "";
public bool e_bip = true;
}