alphatommy_at_hotmail_dot_com said:
[...]
The error that I am getting now is "TargetInvocation Exception was
unhandled" Please advise if your comments match what i did. Also,
how could I resolve the current pop up error. Thanks.
It's hard to say without having the complete code, but I suspect your
exception is happening because the call to BeginInvoke() is incorrect.
The parameters need to be in a single array of objects:
this.BeginInvoke(new Invoke(PortCom1_DataReceived), new object[] {
sender, e});
I don't understand how the code you posted even compiles. There's not
an overload for BeginInvoke that takes three parameters.
Some other comments:
* As I mentioned in my previous post, you shouldn't be drawing in
the invoked method. Instead, all that should happen from the invoked
method is signaling that an update needs to happen by calling
Invalidate(). The data required to correctly draw the form should be
stored somewhere, and the form should handle drawing only in the OnPaint
method or an event handler for the Paint event.
* In addition to the above, the form _must_ be prepared to draw
itself _at any time_. This means that it's not sufficient to just draw
it once directly from the UART thread, even if invoked. The UART thread
must somehow persistently create data that the form can always use in
order to draw itself.
* One possible approach to persisting the data is to go ahead and
let the UART thread do the drawing, but instead of doing so to the form,
draw into a Bitmap object. Once the Bitmap object has been updated,
then the UART thread would use Invoke() to invalidate the form, and the
form would then use the already-drawn Bitmap object to actually draw the
form graphics (ie, a single call to the Graphics.DrawImage() method).
* At the very least, if you decide to ignore all that and continue
to do all of the drawing directly from the UART thread, there is no need
to call Invalidate() before doing the actual drawing. At best it's a
"nop", and at worst (depending on the exact timing) it could result in
the drawing you do being erased immediately after you draw.
* Finally, I'm not a big fan of the "InvokeRequired" pattern. That
is, I create code that either is or is not intended to be called from a
different thread. If it is, then it always calls Invoke() or
BeginInvoke(). If it's not, then it always just does whatever it's
supposed to do. This is a matter of personal preference, but I like to
mention it because MSDN seems so set on this idea of having a single
method handle both the invocation and the actual work. If you really
want a single method that is dual purpose, then just always call
Invoke() or BeginInvoke(). It's not harmful to do so from within the
owning thread.
You can do it MSDN's way without hurting anything; just don't think you
_have_ to do it that way. If you, like I do, feel that having a single
method do two different things is poor design, feel free to break out
the functionality into the code that invokes and the code that does the
work.
Pete