Thread issues

G

Geoff Callaghan

I have an application that is used to control a hardware device through an
RS232 port. I send a command, the hardware responds with a command. There is
also a GUI involved which cannot be frozen while I wait for a response from
the hardware. To achieve this, I use a WaitFor type structure (I'm using a
third party COM control) that allows me to send a command and wait a
specific number of milliseconds until I see one of several responses.

This all works reasonably well for a few minutes. Eventually, however, I get
a null reference exception, and I don't really know where it is coming from.

The commands I send are sent from a threadpool object, which then waits for
a timeout or a response before exiting. There are never more than a few
running at any given time (assuming that the thread exits when the delegate
function is finished, which is how I understand it). The error appears to
happen when the
responses come in.

Am I supposed to explicitly dispose of the threadpoll object, or anything
related? I'm not very familiar with how Visual Basic .NET deals with
threads, and all the blogs and articles I can find are somewhat unclear.
Also, since the thread is using the com object, and the com object is also
being used outside the thread for receiving data, is there a conflict here?
If so, I'm not sure how I would get around it.
 
C

Chris Tacke, eMVP

It's really tough to say without knowing what the third-party stuff is
actually doing. The threads shouldn't need any special attention. If the
NRE is coming from them and you don't have source, then I'd look for support
or another control.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
G

Geoff Callaghan

I'll look into that. I have one further question though: I have a different
delegate sub for each message I am sending. As I understand it, this
shouldn't really matter; I am only launching one at a time, and there aren't
ever more than one running at the same time. Should I send all messages from
one delegate?

Also, I'm not sure I'm using the word "delegate" right. I see it everywhere,
but I haven't been able to find a definition.
 
C

Chris Tacke, eMVP

Inline....
Geoff Callaghan said:
I'll look into that. I have one further question though: I have a
different
delegate sub for each message I am sending. As I understand it, this
shouldn't really matter; I am only launching one at a time, and there
aren't
ever more than one running at the same time. Should I send all messages
from
one delegate?

Shouldn't matter either way. One handler would be cleaner code, and even if
multiple calls can hit it, as long as you make it thread safe it's still
fine.
Also, I'm not sure I'm using the word "delegate" right. I see it
everywhere,
but I haven't been able to find a definition.

How about this?
http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdelegateclasstopic.asp

-Chris
 
G

Geoff Callaghan

Thanks for the reply. I still don't really understand what a delegate is.
Sentences like this: "The Delegate class is not considered a delegate type;
it is a class used to derive delegate types." are enough to drive me out of
the business. I guess I really don't need to know for what I am trying to
accomplish, though, so for now I just won't worry about it.
 
C

Chris Tacke, eMVP

Look up at the top:

"Delegate Class: Represents a delegate, which is a data structure that
refers to a static method or to a class instance and an instance method of
that class."

Basically it's a pointer to a static method or a method of an intantiated
class. In C/C++ terms it's a function pointer.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
C

Carlos Alejandro Pérez

Hi all.
Think about a pointer to a function, this is the essence of a delegate. I´m
coming from the VFP world, so I can understand your point. NET is all about
classes, and the delegate is another new concept we must deal with.
Another nice, simplest definition is "Delegates are objects that can call
the methods of other objects for you". This intermediate objects know the
parameters and types of those other objects, so sometimes are called "type
safe pointers".
hope this help,

Carlos
 
G

Geoff Callaghan

Yup. Thanks.

Carlos Alejandro Pérez said:
Hi all.
Think about a pointer to a function, this is the essence of a delegate. I´m
coming from the VFP world, so I can understand your point. NET is all about
classes, and the delegate is another new concept we must deal with.
Another nice, simplest definition is "Delegates are objects that can call
the methods of other objects for you". This intermediate objects know the
parameters and types of those other objects, so sometimes are called "type
safe pointers".
hope this help,

Carlos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top