CE.Net 4.1 performance

G

Guest

When I run my application (which continuously monitors the serial port) it slows every 30 seconds or so, often pausing for about a second. It shouldn't do this! It's written in VB.Net and uses the .NET CF

It appears to me that there are other services running in the background. Anyone got any advice how I can find out what these are and how I can stop them? Or does anyone have any other ideas what this strange effect is? I can slightly improve matters by setting the thread priority to 'highest' but it doesn't solve the problem completely

Richar
 
A

Alex Feinman [MVP]

You might be running into an issue of garbage collector making a pass. Do
you create a lot of short-lived objects? Example is a polling loop with
local variable(s) defined inside. If this is the case, consider defining the
variable outside the loop

Richard Jones said:
When I run my application (which continuously monitors the serial port) it
slows every 30 seconds or so, often pausing for about a second. It shouldn't
do this! It's written in VB.Net and uses the .NET CF.
It appears to me that there are other services running in the background.
Anyone got any advice how I can find out what these are and how I can stop
them? Or does anyone have any other ideas what this strange effect is? I can
slightly improve matters by setting the thread priority to 'highest' but it
doesn't solve the problem completely.
 
G

Guest

Thanks Alex. I'm going to re-define all the local variables as globals and see what happens.
 
G

Guest

Actually I think I may have found the source of the problem - I have a draw text routine (can't use labels, too many of them!), which is called repeatedly. The code creates lots of local objects and is awash with 'New' statements - it's below. Could anyone give me some pointers as to the best way to correct this to cope with the Garbage Collector? The passed variables pointx, pointy, sizex, sizey and drawtext change obviously

Any help appreciated
And

Public Sub direct_draw_text_small(ByVal text_col As Color, ByVal pointx As Integer, ByVal pointy As Integer, ByVal sizex As Integer, ByVal sizey As Integer, ByVal drawtext As String

' draw text using direct draw technique (called directly from main thread
Dim r As New RectangleF(pointx, pointy, sizex, sizey
Dim rr As New RectangleF(0, 0, sizex, sizey
Dim b As New SolidBrush(text_col

bufferbmp = New Bitmap(sizex, sizey

' to avoid flicker, double buffer onto bitmap then display
Dim gxOff As Graphics = Graphics.FromImage(bufferbmp
gxOff.DrawString(drawtext, glf_font11tahoma, b, rr
gxOff.Dispose(

' dra
Dim gxOn As Graphics = Me.CreateGraphics(
gxOn.DrawImage(bufferbmp, pointx, pointy
gxOn.Dispose(

' manually release graphics objects
b.Dispose(
bufferbmp.Dispose(

End Su
 
A

Alex Feinman [MVP]

A lot of this stuff can be moved outside of this function. You can use a
single, screen-sized bitmap for all your offscreen drawing and keep the
Graphics object associated with it alive at all times. Same goes for
rectangles and brushes. The easiest way would be to create a class for
drawing the text and move all these objects into the member variables
(perhaps static).

Richard Jones said:
Actually I think I may have found the source of the problem - I have a
draw text routine (can't use labels, too many of them!), which is called
repeatedly. The code creates lots of local objects and is awash with 'New'
statements - it's below. Could anyone give me some pointers as to the best
way to correct this to cope with the Garbage Collector? The passed variables
pointx, pointy, sizex, sizey and drawtext change obviously.
Any help appreciated!
Andy


Public Sub direct_draw_text_small(ByVal text_col As Color, ByVal pointx
As Integer, ByVal pointy As Integer, ByVal sizex As Integer, ByVal sizey As
Integer, ByVal drawtext As String)
 
G

Guest

Hi Ale

If I'm using a full-screen sized bitmap won't that slow things down a huge amount if I'mn constantly refreshing the whole screen

Or have I misunderstood

Richar
 
A

Alex Feinman [MVP]

But you can use partial bitmap for drawing - Graphics.DrawImage allows
specifying both source and target rectangle. Think of this bitmap as an
off-screen canvas

Richard Jones said:
Hi Alex

If I'm using a full-screen sized bitmap won't that slow things down a huge
amount if I'mn constantly refreshing the whole screen?
 
G

Guest

Hi Ale
Thanks for the advice... I've done most of what you suggested, but I'm a bit stuck on what to do with the rectangles. I will be drawing various sizes and positions of rectangles; so I guess I need a separate rectangle instance of each size/position? I can't change the position or size of a rectangle after it's been instantiated can I? So will I have to create a new rectangle instance every time the draw method is called, and then dispose of it

Any ideas? I've put the beginnings of the class below so you can get an idea of where I'm going

Cheer

Ric

Public Class DirectDra

' --------------- Declare Class Variables ------------------

' Brushe
Private BrushWhite As New SolidBrush(Color.White
Private Brushcyan As New SolidBrush(Color.Cyan
Private Brushskyblue As New SolidBrush(Color.SkyBlue
Private Brushplum As New SolidBrush(Color.Plum
Private Brushlime As New SolidBrush(Color.Lime
Private Brushyellowgreen As New SolidBrush(Color.YellowGreen
Private Brushspringgreen As New SolidBrush(Color.SpringGreen
Private Brushdodgerblue As New SolidBrush(Color.Gold
Private Brushgold As New SolidBrush(Color.Gold

' Rectangle
' ??

' Font
Private f_arial_10 As New Font("arial", 10, FontStyle.Regular
Private f_tahoma_10 As New Font("tahoma", 10, FontStyle.Regular
Private f_arial_11 As New Font("arial", 11, FontStyle.Regular
Private f_tahoma_11 As New Font("tahoma", 11, FontStyle.Regular
Private f_arial_12 As New Font("arial", 12, FontStyle.Regular
Private f_tahoma_12 As New Font("tahoma", 12, FontStyle.Regular
Private f_tahoma_70 As New Font("tahoma", 70, FontStyle.Regular

' Bitmap
Private bmpOff As New Bitmap(640, 480) ' off-screen bitmap : full scree

' Graphics Object
Private gxOff As Graphics = Graphics.FromImage(bmpOff) ' graphics object associated with offscreen drawin

'Private r As New RectangleF(pointx, pointy, sizex, sizey
'Private rr As New RectangleF(0, 0, sizex, sizey
'Private b As New SolidBrush(text_col

' ------------------- Create Properties ---------------------

' -------------------- Create Methods -----------------------
Public Sub draw_text(ByVal text_col As String, ByVal pointx As Integer, ByVal pointy As Integer, ByVal sizex As Integer, ByVal sizey As Integer, ByVal drawtext As String, ByVal e As System.Windows.Forms.PaintEventArgs

' To avoid flicker, double buffer onto bitmap then display
' clear the section of bitma
' to do : need to handle rectangles..

' decide what colour this is and draw text onto bitma
Select Case text_co
Case "white
gxOff.DrawString(drawtext, f_tahoma_10, BrushWhite, pointx, pointy
Case "cyan
gxOff.DrawString(drawtext, f_tahoma_10, Brushcyan, pointx, pointy
Case "dodgerblue
gxOff.DrawString(drawtext, f_tahoma_10, Brushdodgerblue, pointx, pointy
Case "skyblue
gxOff.DrawString(drawtext, f_tahoma_10, Brushskyblue, pointx, pointy
Case "lime
gxOff.DrawString(drawtext, f_tahoma_10, Brushlime, pointx, pointy
Case "yellowgreen
gxOff.DrawString(drawtext, f_tahoma_10, Brushyellowgreen, pointx, pointy
Case "springgreen
gxOff.DrawString(drawtext, f_tahoma_10, Brushspringgreen, pointx, pointy
Case "plum
gxOff.DrawString(drawtext, f_tahoma_10, Brushplum, pointx, pointy
Case "gold
gxOff.DrawString(drawtext, f_tahoma_10, Brushgold, pointx, pointy

End Selec

' Draw bitmap section onto scree
' this bit draws the appropriate section of bmpOff (written to above) to the passed graphics objec
' r needs to be sorted out, as does the last paramete
e.Graphics.DrawImage(bmpOff, r, pointx, pointy, sizex, sizey, GraphicsUnit.Pixel, 0

End Su

End Class
 
A

Alex Feinman [MVP]

Actually internal represenation of a rectangle consists of X, Y (left top
corner) and cx, cy (dimensions). All 4 are settable independently. THis
means you can use just one rectangle and keep changing its size and position
as needed

Richard Jones said:
Hi Alex
Thanks for the advice... I've done most of what you suggested, but I'm a
bit stuck on what to do with the rectangles. I will be drawing various sizes
and positions of rectangles; so I guess I need a separate rectangle instance
of each size/position? I can't change the position or size of a rectangle
after it's been instantiated can I? So will I have to create a new rectangle
instance every time the draw method is called, and then dispose of it?
Any ideas? I've put the beginnings of the class below so you can get an idea of where I'm going.

Cheers

Rich


Public Class DirectDraw

' --------------- Declare Class Variables -------------------

' Brushes
Private BrushWhite As New SolidBrush(Color.White)
Private Brushcyan As New SolidBrush(Color.Cyan)
Private Brushskyblue As New SolidBrush(Color.SkyBlue)
Private Brushplum As New SolidBrush(Color.Plum)
Private Brushlime As New SolidBrush(Color.Lime)
Private Brushyellowgreen As New SolidBrush(Color.YellowGreen)
Private Brushspringgreen As New SolidBrush(Color.SpringGreen)
Private Brushdodgerblue As New SolidBrush(Color.Gold)
Private Brushgold As New SolidBrush(Color.Gold)

' Rectangles
' ???

' Fonts
Private f_arial_10 As New Font("arial", 10, FontStyle.Regular)
Private f_tahoma_10 As New Font("tahoma", 10, FontStyle.Regular)
Private f_arial_11 As New Font("arial", 11, FontStyle.Regular)
Private f_tahoma_11 As New Font("tahoma", 11, FontStyle.Regular)
Private f_arial_12 As New Font("arial", 12, FontStyle.Regular)
Private f_tahoma_12 As New Font("tahoma", 12, FontStyle.Regular)
Private f_tahoma_70 As New Font("tahoma", 70, FontStyle.Regular)

' Bitmaps
Private bmpOff As New Bitmap(640, 480) ' off-screen bitmap : full screen

' Graphics Objects
Private gxOff As Graphics = Graphics.FromImage(bmpOff) ' graphics
object associated with offscreen drawing
'Private r As New RectangleF(pointx, pointy, sizex, sizey)
'Private rr As New RectangleF(0, 0, sizex, sizey)
'Private b As New SolidBrush(text_col)


' ------------------- Create Properties ----------------------


' -------------------- Create Methods ------------------------
Public Sub draw_text(ByVal text_col As String, ByVal pointx As
Integer, ByVal pointy As Integer, ByVal sizex As Integer, ByVal sizey As
Integer, ByVal drawtext As String, ByVal e As
System.Windows.Forms.PaintEventArgs)
' To avoid flicker, double buffer onto bitmap then display.
' clear the section of bitmap
' to do : need to handle rectangles...

' decide what colour this is and draw text onto bitmap
Select Case text_col
Case "white"
gxOff.DrawString(drawtext, f_tahoma_10, BrushWhite, pointx, pointy)
Case "cyan"
gxOff.DrawString(drawtext, f_tahoma_10, Brushcyan, pointx, pointy)
Case "dodgerblue"
gxOff.DrawString(drawtext, f_tahoma_10, Brushdodgerblue, pointx, pointy)
Case "skyblue"
gxOff.DrawString(drawtext, f_tahoma_10, Brushskyblue, pointx, pointy)
Case "lime"
gxOff.DrawString(drawtext, f_tahoma_10, Brushlime, pointx, pointy)
Case "yellowgreen"
gxOff.DrawString(drawtext, f_tahoma_10, Brushyellowgreen, pointx, pointy)
Case "springgreen"
gxOff.DrawString(drawtext, f_tahoma_10, Brushspringgreen, pointx, pointy)
Case "plum"
gxOff.DrawString(drawtext, f_tahoma_10, Brushplum, pointx, pointy)
Case "gold"
gxOff.DrawString(drawtext, f_tahoma_10, Brushgold, pointx, pointy)

End Select

' Draw bitmap section onto screen
' this bit draws the appropriate section of bmpOff (written to
above) to the passed graphics object
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top