I am trying to create an application that shows the online trains in picturebox
So to implement this i create a worker thread
to get the online train position .so i define the thread as you can see here :
private Thread workerThread = null;
private delegate void UpdateListBoxDelegate();
private UpdateListBoxDelegate UpdateListBox = null;
In Form_load
i call these:
UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus);
// Initialise and start worker thread
workerThread = new Thread(new ThreadStart(this.GetOnlineTrain));
workerThread.Start();
My method that delegate handles that is :
private void UpdateStatus()
{
foreach (TimeTable onlineTrain in OnlineTrainList.ToList())
{
if (lstSensorLeft.Count != 0 || lstSensorRight.Count != 0)
{
pictureBoxonlineTrain.Image = null;
DrawOnlineTrain();
}
else
{
pictureBoxonlineTrain.Image = null;
}
}
this.Invalidate();
}
The GetOnlineTrain
get the position of online train as you can see here :
public void GetOnlineTrain()
{
try
{
while (true)
{
TimeTableRepository objTimeTableREpository = new TimeTableRepository();
OnlineTrainList = objTimeTableREpository.GetAll().ToList();
objTimeTableREpository = null;
Invoke(UpdateListBox);
}
}
catch(Exception a)
{
}
}
And the final function draws the online trains on the picturebox
:
public void DrawOnlineTrain()
{
Bitmap map=null;
if (OnlineTrainList.Count > 0)
{
map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height);
var graph = Graphics.FromImage(map);
foreach (TimeTable t in OnlineTrainList.ToList())
{
// graph.Dispose();
Rectangle rectTrainState = new Rectangle(t.XTrainLocation.Value - 3,
t.YTrainLocation.Value - 3,
15, 15);
graph.FillRectangle(RedBrush, rectTrainState);
graph.DrawString(t.TrainId.ToString(), pictureBoxonlineTrain.Font, Brushes.White, t.XTrainLocation.Value -3, t.YTrainLocation.Value -3);
}
}
pictureBoxonlineTrain.Image = map;
// pictureBoxonlineTrain.Image.Save(@"C:\RailwayShiraz\ShirazMetro\ShirazRailWayWeb\Images\Train.jpg");
}
but my application spends a lot of memory and sometimes i got the Out of memory exception
and sometimes my trains Disappears
from the picturebox
. To draw online train first time i draw the map of trains (lines ,stations ,...) on picturebox
with size x=A
and y=b
after that i create another picturebox
with the same size and put the second picturebox
on first picturebox
using this code:
pictureBoxonlineTrain.Parent = pictureBoxMetroMap;
I think maybe some part of my code consumes a lot of memory and i should use Dispose
or something else .Sometime i got out of memory exception
and the error is caused by graphic
i am not sure !And sometimes i got the error from this line :
map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height);
Could you please give me some help .is there any class that i should dispose that?or the problem is caused by my implementation
I trace the memory usage from taskmanager
and some times my usage reach to 1,666,881
and my application exits.
Best regards