我正在測試我最近完成的DX滾動橫幅。問題是它消耗了大量的內存。C#怎麼了font.DrawText
要滾動的字符串是使用從源字符串(rss提要)創建並存儲在數組中的字符數組及其大小(使用DX度量字符串)創建的。 rss feed在這種情況下大約有500個字符。
要創建滾動字符串,我只需在顯示面板上添加/刪除字符作爲進入/離開視圖(使用面板寬度&字符大小以確定添加/刪除字符串中的字符的時間)。它工作得很好,但使用200M的內存。字符串永遠不會超過80個字符長(如果超過80個字符,我會通過使用警報來確保這一點)。字符串中的字符數量當然取決於字體的大小。
如果我註釋掉DrawText命令,應用程序會使用很少的內存(證明它不是代碼的其他部分)。如果我只是滾動整個字符串(500個字符),則使用的內存只有32M。但是,我現在正在使用大量的Proc滾動這麼大的字符串。我注意到當你繪製一個靜態(不滾動)的大字符串,然後跟着它,例如單個字符字符串,DrawText不釋放用於繪製大字符串的內存。我正在使用theString.Remove & theString.Insert創建要滾動的字符串。內存似乎隨着每個字符的增加/減少而上升,並且一旦整個RSS提要字符串被滾動,內存使用保持靜態 - 在200M處 - 從此開始。
發生了什麼事?非常感謝任何幫助......這讓我瘋狂!我可以將feed分解爲字符串,但更有意義的是按字符進行。
private void sync()
{
if (device.RasterStatus.ScanLine)
{
UpdateDisp();
if (ArStringSegments.Count != 0)
{
for (int i = 0; i != Speed; i++)
{
# region Add a character to the displayed string
if (FirstCharLength == 0 && AddChrIndex != ArStringSegments.Count)
{
StringProps StringProps = (StringProps)ArStringSegments[AddChrIndex];
if (ScrollDirection == Direction.ToLeft)
{
theString = theString.Insert(theString.Length, StringProps.String);
}
AddChrIndex++;
}
# endregion Add a character to the string
# region remove a character from the string as it goes beyond the veiwable area
if (RemoveChrIndex != ArStringSegments.Count)
{
if (ScrollDirection == Direction.ToLeft)
{
if(ScrollInc == 0 - LargestChar)
{
StringProps RemoveString = (StringProps)ArStringSegments[RemoveChrIndex];
theString = theString.Remove(0, 1);
ScrollInc += RemoveString.Size1;
RemoveChrIndex++;
}
}
}
# endregion remove a character from the string as it goes beyond the veiwable area
# region Increment/decrement position
if (ScrollDirection == Direction.ToLeft)
{
ScrollInc--;
FirstCharLength--;
}
# endregion Increment/decrement position
# region Entire string has gone out of viewable area, scroll out by an amount and then start again.
if ((ScrollInc == 0 - (ScrollOutLength + LargestChar) && ScrollDirection == Direction.ToLeft) ||
(ScrollInc == PanWidth + (ScrollOutLength + LargestChar) && ScrollDirection == Direction.ToRight))
{
theString = "";
AddChrIndex = 0;
RemoveChrIndex = 0;
FirstCharLength = 0;
if (ScrollDirection == Direction.ToLeft)
{
ScrollInc = this.PanWidth;
}
else
{
ScrollInc = 0;
RightBoundary = 0;
}
}
# endregion entire string has gone out of viewable area, scroll out by an amount and then start again.
}
}
}
ScanCount = device.RasterStatus.ScanLine;
}
private void UpdateDisp()
{
try
{
if (device.CheckCooperativeLevel(out DeviceStatus))
{
if (UpdateDisplayEnabled == true)
{
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();
// background image Texture
BackSprite = new Sprite(device);
BackSprite.Begin(SpriteFlags.AlphaBlend);
BackSprite.Draw2D(PanelTexture, new Point(0, 0), 0.0F, new Point(0, 0), Color.White);
BackSprite.End();
BackSprite.Dispose();
// draw the text
Draw2DText(FeedText[i], ScrollInc, 0, FontColor);
font.DrawText(null, theString, new Rectangle(ScrollInc, 0,
device.EndScene();
if (device.CheckCooperativeLevel(out DeviceStatus)) device.Present((IntPtr)DxRenderPanel);
}
}
}
這發送更新每次掃描行數達到0 – user183185 2009-11-12 18:24:01