Communicating between threads

G

Guest

Hi friends,

How to communicate between two threads ?

Scenario:

I have two thread, when any one thread has done some changes , this changes
should be reflect on the other thread. So mutual understanding between thread
is necessary.

Is it possible to resolve this problem by using Inter process communication
[IPC]?

If yes please provide me some sample code snippet.

Thanks and regards,
Loyola stalin. S
 
K

Kevin Spencer

Use events.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
P

Peter Duniho

[...]
I have two thread, when any one thread has done some changes , this
changes should be reflect on the other thread. So mutual
understanding between thread is necessary.

Is it possible to resolve this problem by using Inter process
communication
[IPC]?

It depends on what you want to do. First, I'll point out that you
definitely *don't* need IPC just for communicating between threads.
Threads have access to the same memory address space, and so can
communicate directly via data structures and variables.

IMHO, Kevin's answer isn't as helpful as it could be, mostly because the
word "events" describes two different things, one of which
(WaitHandle-based events) is directly useful for inter-thread
communications, and one of which (.NET events, using the "event" C#
keyword) is useful only in conjunction with other mechanisms designed for
inter-thread communications.

I personally do like using .NET events for communicating between objects,
whether in the same thread or not. They are great when you have something
in one object for which you want another object to be informed when it
happens. That is, an "event" occurs. :) But they don't inherently offer
any cross-thread protection. Depending on what you're trying to
communicate, however, they are a good starting place (as Kevin suggests).

The main thing though is to be aware of a couple of things:

* You cannot use UI elements (Control-derived objects, mainly) across
threads directly. Anything that interacts with the underlying Windows
window object has to be executed on the same thread that created that
window object. That includes practically every method and property
defined in .NET for an object derived from Control. To access these, you
need to use Form.Invoke() or Form.BeginInvoke() to provide a delegate that
will be run on the same thread that created the object.

* For other communications, you need to protect the data being used to
communicate with some type of synchronization, so ensure that no other
thread is trying to read the data while one thread is writing to it, and
to ensure that all threads are always using the most up-to-date data. In
some cases, you can use a .NET object designed for inter-thread
communication directly for the communication, or you can use one of those
objects to control access to other data to ensure synchronization.

The "volatile" keyword can be used to ensure that each thread is using
up-to-date data, while objects like WaitHandle-based events, semaphores,
mutexes can be used for synchronization, as well as the "lock()" statement
in C# (I don't know if VB.NET or other .NET languages have something
similar, but I'd guess they do).

So, there's the basics. For anything more specific, we'd need to know
exactly what it is you're trying to communicate between threads. As an
example, given your vague description it is *possible* that it would be
sufficient to use an AutoResetEvent object that is set when the one thread
is done making whatever changes it wants to make, signaling to the other
thread (which would be stopped at a statement calling
AutoResetEvent.WaitOne()) that the changes have been done.

Pete
 
J

Jon Skeet [C# MVP]

Loyola stalin said:
How to communicate between two threads ?

Using monitors and WaitHandles, along with shared data structures,
usually.
Scenario:

I have two thread, when any one thread has done some changes , this changes
should be reflect on the other thread. So mutual understanding between thread
is necessary.

It's not clear what you're changing, or what the threads involved are
doing. Could you give more information?

See http://pobox.com/~skeet/csharp/threads for a general threading
tutorial.
Is it possible to resolve this problem by using Inter process communication
[IPC]?

IPC is used for communicating between different *processes*, not
between different threads.
 
G

Guest

It's not clear what you're changing, or what the threads involved are
doing. Could you give more information?

While i am copying the data into the hard disk, i want to show the
status[How much % of work is completed] in the UI. In any case both thread
should not stop their process.
 
P

Peter Duniho

While i am copying the data into the hard disk, i want to show the
status[How much % of work is completed] in the UI. In any case both
thread should not stop their process.

You might want to look at the BackgroundWorker class, which includes a
ProgressChanged event your UI can subscribe to in order to be informed of
progress in the background thread.

There are, of course, other ways to accomplish the same but in this case
it seems that what you're trying to do is specifically addressed by an
existing .NET class. At the very least, if you read the documentation for
BackgroundWorker, you may find some useful information regarding the
general concepts. I think it likely you'll find it's just the class you
need.

Pete
 
G

Guest

Hi guys,

Not sure if it's helpful or not, but here is a link to the best article I've
seen on threading:

http://www.yoda.arachsys.com/csharp/threads/

....the author is modest, but seems to really know his stuff.

Cheers,

Steve.

Peter Duniho said:
While i am copying the data into the hard disk, i want to show the
status[How much % of work is completed] in the UI. In any case both
thread should not stop their process.

You might want to look at the BackgroundWorker class, which includes a
ProgressChanged event your UI can subscribe to in order to be informed of
progress in the background thread.

There are, of course, other ways to accomplish the same but in this case
it seems that what you're trying to do is specifically addressed by an
existing .NET class. At the very least, if you read the documentation for
BackgroundWorker, you may find some useful information regarding the
general concepts. I think it likely you'll find it's just the class you
need.

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