Since I can't find any control to use as a LED indicator on my winform for my serial communication transmit and receive, I created my own user-defined indicator from label. It basically sets and resets the color of a label from black to lime for receive and black to red for transmit repeatedly. Its class is as follows. However, my .NET program seem to run for couple of hours and crash completely. When I view the details for crash error, windows reports it as clr20r3 error. I've had a similar issue when I wrote and developed a program under fedora linux. My serial communication indicator on a form somehow caused memory leak and crashed the program and when it was removed it worked flawlessly.
So, can you have memory leak from setting and resetting backcolor of a label repeatedly within seconds?
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;
This is how it looks on a winform:
Since no one wants to answer and I have solved my issue, I will answer this question myself.
Yes, it can lead to memory leak from what I have experienced and it depends how you set the backcolor for your control. If your control is not placed on the winform, then setting and resetting backcolor within short amount of time can lead to memory leak as Hans Passant has said. So, I followed his advice.
Basically I placed my control right on the winform and from my thread, I set and reset the backcolor. So far it has worked. I have been running my program for the last 5 days nonstop and it hasn't crashed or lost control.