|
-
Mar 10th, 2010, 05:43 PM
#9
Re: Class events question
The solution works, but I don't like it.
The serial port already raises a datareceived event. Since you are wrapping the port in a different class, do you really want to have that class deal with events on different threads? Do you want any class (form) that uses your wrapper class to have to be aware that the datareceived event is coming in on a different thread? I would say that it would be better to raise the event on the UI thread and be done with it. Otherwise, what is the advantage to wrapping the serial port object?
If you would prefer to raise the event on the UI thread, take a look at the UDP class I posted at about #7 in this thread:
http://www.vbforums.com/showthread.php?t=509619
In particular, look at how I used the SynchronizationContext. I save the context before starting the thread (you could do that in your class constructor, which will mean that the context saved will be for the thread that created the object, though the object is not thread specific). You then add a sub that raises the actual event, and when you want to raise the event, you post a call to that sub on the context. It takes only a couple lines, and your events come out on the thread that created the class.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|