473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_s ensor() 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 .SerialDataRece ivedEventArgs e)
{
string Mystring = sp.ReadExisting ();
foreach (byte c in Mystring)
{
if(pixelcount<1 02)
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.FromIm age(m_objDrawin gSurface);

When it hits the last line above I get
InvalidOperatio nException 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.InvalidO perationExcepti on was unhandled
Message="Object is currently in use elsewhere."
Source="System. Drawing"
StackTrace:
at System.Drawing. Graphics.FromIm age(Image image)
at Laserizer_contr ol.Form1.DrawCh art(Int32 x, Int32 y) in E:
\Laserizer control\Laseriz er control\Form1.c s:line 146
at Laserizer_contr ol.Form1.sp_Dat aReceived(Objec t sender,
SerialDataRecei vedEventArgs e) in E:\Laserizer control\Laseriz er
control\Form1.c s:line 96
at System.IO.Ports .SerialPort.Cat chReceivedEvent s(Object src,
SerialDataRecei vedEventArgs e)
at
System.IO.Ports .SerialStream.E ventLoopRunner. CallReceiveEven ts(Object
state)
at
System.Threadin g._ThreadPoolWa itCallback.Wait Callback_Contex t(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at
System.Threadin g._ThreadPoolWa itCallback.Perf ormWaitCallback (Object
state)
Thanks
Ringo

Feb 28 '07 #1
2 2982
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*********@gm ail.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.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_s ensor() 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 .SerialDataRece ivedEventArgs e)
{
string Mystring = sp.ReadExisting ();
foreach (byte c in Mystring)
{
if(pixelcount<1 02)
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.FromIm age(m_objDrawin gSurface);

When it hits the last line above I get
InvalidOperatio nException 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.InvalidO perationExcepti on was unhandled
Message="Object is currently in use elsewhere."
Source="System. Drawing"
StackTrace:
at System.Drawing. Graphics.FromIm age(Image image)
at Laserizer_contr ol.Form1.DrawCh art(Int32 x, Int32 y) in E:
\Laserizer control\Laseriz er control\Form1.c s:line 146
at Laserizer_contr ol.Form1.sp_Dat aReceived(Objec t sender,
SerialDataRecei vedEventArgs e) in E:\Laserizer control\Laseriz er
control\Form1.c s:line 96
at System.IO.Ports .SerialPort.Cat chReceivedEvent s(Object src,
SerialDataRecei vedEventArgs e)
at
System.IO.Ports .SerialStream.E ventLoopRunner. CallReceiveEven ts(Object
state)
at
System.Threadin g._ThreadPoolWa itCallback.Wait Callback_Contex t(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at
System.Threadin g._ThreadPoolWa itCallback.Perf ormWaitCallback (Object
state)
Thanks
Ringo

Mar 1 '07 #2
On Mar 1, 7:11 am, "Kevin Spencer" <unclechut...@n othinks.comwrot e:
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...@gm ail.comwrote in message

news:11******** **************@ p10g2000cwp.goo glegroups.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_s ensor() 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 .SerialDataRece ivedEventArgs e)
{
string Mystring = sp.ReadExisting ();
foreach (byte c in Mystring)
{
if(pixelcount<1 02)
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.FromIm age(m_objDrawin gSurface);
When it hits the last line above I get
InvalidOperatio nException 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.InvalidO perationExcepti on was unhandled
Message="Object is currently in use elsewhere."
Source="System. Drawing"
StackTrace:
at System.Drawing. Graphics.FromIm age(Image image)
at Laserizer_contr ol.Form1.DrawCh art(Int32 x, Int32 y) in E:
\Laserizer control\Laseriz er control\Form1.c s:line 146
at Laserizer_contr ol.Form1.sp_Dat aReceived(Objec t sender,
SerialDataRecei vedEventArgs e) in E:\Laserizer control\Laseriz er
control\Form1.c s:line 96
at System.IO.Ports .SerialPort.Cat chReceivedEvent s(Object src,
SerialDataRecei vedEventArgs e)
at
System.IO.Ports .SerialStream.E ventLoopRunner. CallReceiveEven ts(Object
state)
at
System.Threadin g._ThreadPoolWa itCallback.Wait Callback_Contex t(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at
System.Threadin g._ThreadPoolWa itCallback.Perf ormWaitCallback (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
2389
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 graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store it as member variable of the form , make
14
3104
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 to do this? E.g. I want to be able to graph a function. At this point I'm not up to a level in C++ where I want to start learning Visual C++ so I don't want to go that route. Thanks
2
1679
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 make some interesting apps which could tranform images (i.e. making saturation of colours "heavy", or gradually fade to grayscale, or "erasing" a colour... and so on), while nowadays it seems quite impossible. Now that I got .NET over...
8
3333
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 froggraphic As Graphics = Graphics.FromImage(frogbitmap) froggraphic.RotateTransform(90) frogbitmap.Save(Server.MapPath("images/frog2.gif"), Imaging.ImageFormat.Gif)
7
2584
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 stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
15
1854
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
4273
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 Graphics object, which is the same as if I'd need to print it. It'd be easy using Control.DrawToBitmap, but I also need the invisible part of the panel (which is hidden because of scrolling) and DrawToBitmap just takes a screenshot.
1
1403
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 wrote a function called Draw_sonar2 that does all the drawing. It usually works but sometimes crashes saying the graphics object is already in use. After digging I saw that I should be doing the Drawing in the PB.paint, not a seperate function....
9
5067
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 passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.