473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Graphics help

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
2 2958
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
14
by: Pmb | last post by:
At the moment I'm using Borland's C++ (http://www.borland.com/products/downloads/download_cbuilder.html#) I want to be able to take an array of points and plot them on the screen. Is there a way...
2
by: Tamer Abdalla via DotNetMonster.com | last post by:
Hello, everyone! I DO need some help in order to understand how to create graphics in VB.NET. I'm a little bit confused... I once knew a time when using Point & PSet was almost the only way to...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
9
by: she_prog | last post by:
Dear All, I need to save the content of a panel to a bitmap. The panel can have many child controls which also need to be saved. The problem would be solved if I could have the panel saved to a...
1
by: Ringo | last post by:
I need some help. I have an app that talks to a sonar board via the serial port. In my sp_DataReceived I gather all the data and put it into arrays. Then I want to draw the data in a pictureBox. I...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.