由于我在我的 winform 上找不到任何控件可用作我的串行通信发送和接收的 LED 指示灯,因此我从标签创建了我自己的用户定义指示灯。它基本上设置和重置标签的颜色,从黑色到石灰用于接收,从黑色到红色用于重复传输。它的类如下。但是,我的 .NET 程序似乎运行了几个小时并完全崩溃。当我查看崩溃错误的详细信息时,Windows 将其报告为clr20r3 error。当我在 fedora linux 下编写和开发程序时,我遇到了类似的问题。我在表单上的串行通信指示器不知何故导致内存泄漏并使程序崩溃,当它被删除时,它工作得完美无缺。
那么,你会在几秒钟内重复设置和重置标签的背景色而导致内存泄漏吗?
namespace SerialLED;
interface
uses
System.Collections.Generic,
System.Windows.Forms,
System.Drawing.*,
System.Text;
type
TheLED = public class(Label)
private
protected
public
constructor;
end;
TSerialIndicator = public class
private
method TxTimerEvent(Sender:System.Object; e:System.EventArgs);
method RxTimerEvent(Sender:System.Object; e:System.EventArgs);
public
Txlight:TheLED;
Rxlight:TheLED;
TxTimer:System.Timers.Timer;
RxTimer:System.Timers.Timer;
constructor(mform:Form);
method Transmit;
method Receive;
end;
implementation
method TSerialIndicator.Transmit;
begin
TxLight.BackColor := Color.Red;
if TxTimer.Enabled = false then
TxTimer.Enabled:=true;
end;
method TSerialIndicator.Receive;
begin
RxLight.BackColor := Color.Lime;
if RxTimer.Enabled=false then
RxTimer.Enabled:=true;
end;
method TSerialIndicator.RxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
RxLight.BackColor := Color.Black;
RxTimer.Enabled:=false;
end;
method TSerialIndicator.TxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
TxLight.BackColor := Color.Black;
TxTimer.Enabled:=false;
end;
constructor TSerialIndicator(mform:Form);
begin
RxLight := new TheLED;
TxLight := new TheLED;
TxLight.AutoSize := false;
RxLight.AutoSize := false;
TxLight.BorderStyle := BorderStyle.Fixed3D;
RxLight.BorderStyle := BorderStyle.Fixed3D;
TxLight.Location := new point(52,163);
RxLight.Location := new point(82,163);
TxLight.Width := 20;
TxLight.Height := 20;
RxLight.Width :=20;
RxLight.Height := 20;
mform.Controls.Add(RxLight);
mform.Controls.Add(TxLight);
RxTimer := new System.Timers.Timer;
TxTimer := new System.Timers.Timer;
RxTimer.Interval:=50;
TxTimer.Interval:=50;
RxTimer.Enabled:=false;
TxTimer.Enabled:=false;
RxTimer.Elapsed += new System.Timers.ElapsedEventHandler(@RxTimerEvent);
TxTimer.Elapsed += new System.Timers.ElapsedEventHandler(@TxTimerEvent);
RxLight.BackColor := Color.Black;
TxLight.BackColor := Color.Black;
end;
constructor TheLED;
begin
self.DoubleBuffered:=true;
end;
这是它在winform上的样子: