出在C#内存异常绘制在PictureBox的火车时, [英] Out of memory exception in c# when drawing trains in picturebox
问题描述
我想创建一个显示在网上火车的应用程序的 图片框
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;
在 Form_load中
我把这些:
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();
}
的 GetOnlineTrain
上网列车的位置,你可以在这里看到:
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)
{
}
}
和最终的功能借鉴了 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");
}
但我的应用程序花费了大量的记忆,有时我得到了内存不够的异常
键,有时我的火车消失
从图片框
。要绘制在线列车第一次我画的火车在地图上图片框(线路,车站,...)
与大小 X = A
和计算y = b
之后,我创建另一个图片框
具有相同的大小,并把第二个图片框
首次图片框
:
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;
我想也许我的code的某些部分会消耗大量的内存和我应该使用的Dispose
或别的东西.Sometime我得到内存不够的异常
和错误是造成图形
我不知道,有时我得到了来自该行的错误:
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
我从跟踪任务管理器
的内存使用情况和有时我的使用达到 1666881
和我的应用程序退出。
I trace the memory usage from taskmanager
and some times my usage reach to 1,666,881
and my application exits.
最好的问候
推荐答案
废弃的位图,你不再需要。他们占用非托管内存的巨额资金。该GC不知道非托管内存,并基于可达非托管内存不能触发集合。
Dispose bitmaps which you no longer need. They consume huge amounts of unmanaged memory. The GC is not aware of unmanaged memory and cannot trigger a collection based on unreachable unmanaged memory.
这篇关于出在C#内存异常绘制在PictureBox的火车时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!