473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

threads co-ordinating access to a method

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
Mar 27 '07 #1
3 1489
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Petedwrote in message news:45********************************@4ax.com...
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

Mar 27 '07 #2
On Mon, 26 Mar 2007 19:58:46 -0700, <Petedwrote:
[...]
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
Mar 27 '07 #3
thanks for the advice
On Tue, 27 Mar 2007 10:58:46 +0800, Peted wrote:
>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
Mar 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tzach | last post by:
I'm developing a simple Java client that runs over a CORBA server. The main client thread is waiting for notification from this server. On each notification, The client creates a new thread...
6
by: Thomas Womack | last post by:
If I have a dual-processor hyperthreaded machine (so with four CPU contexts), will a python program distribute threads over all four logical processors? I ask because I'm fairly sure that this...
3
by: Zunbeltz Izaola | last post by:
Hi I have create two threads (from threading module). I want to synchronize this two in the folowwing way def Threa1func(): do stuff.. while something if test: CHANGE TO Thread2
6
by: Simon Harvey | last post by:
Hi everyone, I need to make a service that monitors a directory for changes in the files contained within it. I have two questions: 1. I'm going to be using a FileSystemWatcher object to do...
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
18
by: stephen | last post by:
Hi Just learning c and wonder if anyone can answer a question. I am writing a program that uses threads. These threads will call a specific function simultaneously. This specific function...
11
by: vzaffiro | last post by:
Does the magic number of 25 only apply to the thread pool? Does it also apply to manually creating threads?
3
by: panic | last post by:
hello, In my web app when some button is clicked, a new thread is created to import data and that thread creates some threads too, inside those threads some session variables are updated, but...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.