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
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