Graphics help

R

Ringo

Please excuse the long post, but I have an issue and don;t know how to
describe it without describing the app.

I have an app that reads data fro the serial port and draws it on the
screen. I have a timer set up, and when the timer fires it either
calls Simulate_read_sensor() or read_sensor() depending on a
checkbox.
The simulate function does this
for (x = 0; x < 102; x ++)
{
y = randomGenerator.Next(0, 255);
DrawChart(x, y);
}

and the real function sends a few bytes out eh serial port to request
data. When the data comes in this happens.
void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Mystring = sp.ReadExisting();
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
}

if (pixelcount == 102)
{
pixelcount = 0;
for (int x = 0; x < 102; x ++)
{
y = pixel[x];
DrawChart(x, y);
}
oldx = 0;
}
}


As you can see, all it does is read the bytes and call DrawChart(x,
y);
My issue is that when I'm doing the simulate, everything is fine, but
when I'm doing the real thing then DrawChart() crashes.



Drawchart starts off like this
private void DrawChart(int x, int y)
{
Graphics objGraphics;
Rectangle rectBounds;
int newx, newy;
newx = x * 4;
newy = y ;
objGraphics = Graphics.FromImage(m_objDrawingSurface);

When it hits the last line above I get
InvalidOperationException was unhandled
Object is currently in use elsewhere.
If you are using the Graphics object after the GetHDC method, call the
ReleaseHDC method.

I'm totally confused by this. Why does it crash with real data but not
with simulated data. If anyone can help please tell me what to do, and
use small words, I'm a hardware guy, not a programmer :)


Here are the gory details of the crash message.
System.InvalidOperationException was unhandled
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.FromImage(Image image)
at Laserizer_control.Form1.DrawChart(Int32 x, Int32 y) in E:
\Laserizer control\Laserizer control\Form1.cs:line 146
at Laserizer_control.Form1.sp_DataReceived(Object sender,
SerialDataReceivedEventArgs e) in E:\Laserizer control\Laserizer
control\Form1.cs:line 96
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object
state)


Thanks
Ringo
 
K

Kevin Spencer

What you need to do is, rather than trying to draw the chart whenever you
receive the data, draw the chart in the OnPaint event method for the Control
that displays it. Your methods that read the data from the port should not
attempt to draw directly to the User Interface. Instead, they should store
the data used to do the drawing. The OnPaint method is called by the OS
whenever the Control needs to be redrawn. If you want to update the Control,
call Invalidate on it to force it to repaint itself. The problem here is
that your code is contending for the graphics context used by the OS to
paint with.

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Ringo said:
Please excuse the long post, but I have an issue and don;t know how to
describe it without describing the app.

I have an app that reads data fro the serial port and draws it on the
screen. I have a timer set up, and when the timer fires it either
calls Simulate_read_sensor() or read_sensor() depending on a
checkbox.
The simulate function does this
for (x = 0; x < 102; x ++)
{
y = randomGenerator.Next(0, 255);
DrawChart(x, y);
}

and the real function sends a few bytes out eh serial port to request
data. When the data comes in this happens.
void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Mystring = sp.ReadExisting();
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
}

if (pixelcount == 102)
{
pixelcount = 0;
for (int x = 0; x < 102; x ++)
{
y = pixel[x];
DrawChart(x, y);
}
oldx = 0;
}
}


As you can see, all it does is read the bytes and call DrawChart(x,
y);
My issue is that when I'm doing the simulate, everything is fine, but
when I'm doing the real thing then DrawChart() crashes.



Drawchart starts off like this
private void DrawChart(int x, int y)
{
Graphics objGraphics;
Rectangle rectBounds;
int newx, newy;
newx = x * 4;
newy = y ;
objGraphics = Graphics.FromImage(m_objDrawingSurface);

When it hits the last line above I get
InvalidOperationException was unhandled
Object is currently in use elsewhere.
If you are using the Graphics object after the GetHDC method, call the
ReleaseHDC method.

I'm totally confused by this. Why does it crash with real data but not
with simulated data. If anyone can help please tell me what to do, and
use small words, I'm a hardware guy, not a programmer :)


Here are the gory details of the crash message.
System.InvalidOperationException was unhandled
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.FromImage(Image image)
at Laserizer_control.Form1.DrawChart(Int32 x, Int32 y) in E:
\Laserizer control\Laserizer control\Form1.cs:line 146
at Laserizer_control.Form1.sp_DataReceived(Object sender,
SerialDataReceivedEventArgs e) in E:\Laserizer control\Laserizer
control\Form1.cs:line 96
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object
state)


Thanks
Ringo
 
R

Ringo

What you need to do is, rather than trying to draw the chart whenever you
receive the data, draw the chart in the OnPaint event method for the Control
that displays it. Your methods that read the data from the port should not
attempt to draw directly to the User Interface. Instead, they should store
the data used to do the drawing. The OnPaint method is called by the OS
whenever the Control needs to be redrawn. If you want to update the Control,
call Invalidate on it to force it to repaint itself. The problem here is
that your code is contending for the graphics context used by the OS to
paint with.

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net




Please excuse the long post, but I have an issue and don;t know how to
describe it without describing the app.
I have an app that reads data fro the serial port and draws it on the
screen. I have a timer set up, and when the timer fires it either
calls Simulate_read_sensor() or read_sensor() depending on a
checkbox.
The simulate function does this
for (x = 0; x < 102; x ++)
{
y = randomGenerator.Next(0, 255);
DrawChart(x, y);
}
and the real function sends a few bytes out eh serial port to request
data. When the data comes in this happens.
void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Mystring = sp.ReadExisting();
foreach (byte c in Mystring)
{
if(pixelcount<102)
pixel[pixelcount] = c;
pixelcount++;
}
if (pixelcount == 102)
{
pixelcount = 0;
for (int x = 0; x < 102; x ++)
{
y = pixel[x];
DrawChart(x, y);
}
oldx = 0;
}
}
As you can see, all it does is read the bytes and call DrawChart(x,
y);
My issue is that when I'm doing the simulate, everything is fine, but
when I'm doing the real thing then DrawChart() crashes.
Drawchart starts off like this
private void DrawChart(int x, int y)
{
Graphics objGraphics;
Rectangle rectBounds;
int newx, newy;
newx = x * 4;
newy = y ;
objGraphics = Graphics.FromImage(m_objDrawingSurface);
When it hits the last line above I get
InvalidOperationException was unhandled
Object is currently in use elsewhere.
If you are using the Graphics object after the GetHDC method, call the
ReleaseHDC method.
I'm totally confused by this. Why does it crash with real data but not
with simulated data. If anyone can help please tell me what to do, and
use small words, I'm a hardware guy, not a programmer :)
Here are the gory details of the crash message.
System.InvalidOperationException was unhandled
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.FromImage(Image image)
at Laserizer_control.Form1.DrawChart(Int32 x, Int32 y) in E:
\Laserizer control\Laserizer control\Form1.cs:line 146
at Laserizer_control.Form1.sp_DataReceived(Object sender,
SerialDataReceivedEventArgs e) in E:\Laserizer control\Laserizer
control\Form1.cs:line 96
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object
state)
Thanks
Ringo- Hide quoted text -

- Show quoted text -

ok, that makes sense, I'll give it a try,
Thanks
Ringo
 

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