threads co-ordinating access to a method

P

Peted

Hi i hope i explaine this correctly

I have a class with a method that reads and writes using blocking
sockets to a TCP device. Call it class IPdevice. I can use this
method to send commands and then wait for a response from this
ipdevice im connected to

i want to have a background worker thread on a form that once a minute
sends a command to retrieve the current time from this ip device then
update it into the form display.

Whats the best way to handle gui update from a worker thread ?

and

how can i call thie method in the IPdevice class from the worker
thread without casuing a conflict when the same method is called from
a button click on the form ?


thanks for any advice

Peted
 
N

Nicholas Paldino [.NET/C# MVP]

Peted,

In order to make a call to the UI from another thread, you have to pass
a delegate to the Invoke method on a control that is on the UI thread. The
delegate will be called on the UI thread to perform whatever UI updates you
need.

To synchronize access to a method, you can use the lock statement to
lock on an object to make sure it is called only once at any particular
point in time. You can also look at the MethodImplAttribute (passing the
MethodImpl.Synchronized enumeration member) to synchronize access to a
method. However, the latter solution is not the best, because you are
exposing the object which is being locked on (the instance itself) which
violates general best practice principals regarding encapsulation.

Hope this helps.
 
P

Peter Duniho

[...]
i want to have a background worker thread on a form that once a minute
sends a command to retrieve the current time from this ip device then
update it into the form display.

Whats the best way to handle gui update from a worker thread ?

See Invoke and BeginInvoke. You can invoke a delegate on the main GUI
thread, by calling one of those methods from the worker thread. The
delegate would be passed whatever data you need for the update, and would
update the GUI as necessary.
how can i call thie method in the IPdevice class from the worker
thread without casuing a conflict when the same method is called from
a button click on the form ?

It's not clear from your post what functionality you actually need.
However, assuming you have a single class (IPdevice) that handles i/o with
your device, but you have multiple users of that class, I see at least a
couple of options that might work.

One is to simply synchronize access to the class. Assuming your i/o is
strictly of the "query/response" sort, and responses occur in a
nearly-immediate manner, this would probably work fine (you'd wind up with
threads waiting not only on their own queries, but those of other threads,
so if the queries aren't nearly-immediate the delays would be more
noticeable). You would take a lock at the beginning of any
"query/response" sequence. This would allow your worker thread and main
thread to both use the IPdevice while ensuring that only one at a time is
actually communicating with your device.

Another option would be to create an i/o queue that stores the query and a
delegate to be called when the response is received. This would still
need to be synchronized, but it would allow the client threads (the main
GUI thread and the worker thread) to do other things while waiting for the
i/o to complete. It doesn't sound like this would be required in your
case, but it's something to consider if my impression of your situation is
incorrect. (Note that this option is very similar to the already-existing
paradigm provided by the network i/o classes in the "async with callback"
methods...you'd have to implement your own layer on top of that, to deal
with the fact that you have different clients of the IPdevice class, but
you can look at the .NET version for an idea of how this would work)

These are not necessarily the only solutions...they just happen to be what
came to mind for me first.

Pete
 

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