By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

Graphics help

P: n/a
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(Obj ect src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallR eceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallb ack_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWa itCallback(Object
state)
Thanks
Ringo

Feb 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ri*********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
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(Obj ect src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallR eceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallb ack_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWa itCallback(Object
state)
Thanks
Ringo

Mar 1 '07 #2

P: n/a
On Mar 1, 7:11 am, "Kevin Spencer" <unclechut...@nothinks.comwrote:
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" <ringo.da...@gmail.comwrote in message

news:11**********************@p10g2000cwp.googlegr oups.com...
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(Obj ect src,
SerialDataReceivedEventArgs e)
at
System.IO.Ports.SerialStream.EventLoopRunner.CallR eceiveEvents(Object
state)
at
System.Threading._ThreadPoolWaitCallback.WaitCallb ack_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWa itCallback(Object
state)
Thanks
Ringo- Hide quoted text -

- Show quoted text -
ok, that makes sense, I'll give it a try,
Thanks
Ringo

Mar 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.