Thread marshalling

G

Guest

I apologize in advance for the foolishness of this question, but I have not
been able to find an answer anywhere.

I’m just starting out with threading in .NET and have come across the terms,
“thread marshalling†on a few occasions.

What exactly is thread marshalling? Is it the process of packaging and
sending method parameters across thread boundaries? How is marshalling used
in a threading context?

Thanks in advance.
 
G

Guest

Hey Chris,

Yes, basically thread marshalling is about serializing and deserializing
method parameters when you cross thread boundaries.

In the days before .net (when using COM) marshalling was very important when
ever you called a COM object across thread boundaries and you had to take the
so-called threading model into account whenever you implemented a new COM
component. Depending on which threading model you component supported you
had to provide appropriate synchronization mechanisms in your implementation.

In .net marshalling is still very important but normally no marshalling is
necessary when calling across thread boundaries. But if you suspect your
class might be called from different threads at the same time, you must
provide some kind of synchronization in your implementation (e.g. by using
the System.Threading.Monitor type).

However, marshalling is required when crossing AppDomains in .net - e.g.
when using remoting. It is also required when calling windows forms Controls
from other threads than the thread owning the control (see the docs for
System.Windows.Forms.Control.Invoke).

I hope this shedded some light on the issue. Threading is complicated and
if you are new to .net, threading is probably not a good place to start :)

Regards, Jakob.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jakop,

Just a little correction. There is no need to marshal data (parameters)
across thread boundaries. Data is marshaled only when the call is made
between process boundaries (in this case you always cross thread boundaries
as well). In .NET there is one more boundries across which the data needs to
be marchaled; those are AppDomain boundaries.
When one talks about marchaling across thread boundaries noramlly it means
to schedule a piece of code to be executed by the target thread. It is
natural for threads working in event driven fashion. That's why the only
thread marshaling that .NET support is with UI threads (Control.Invoke and
Control.BeginInvoke).
This is the meaning of thread apartments in COM. However there might be need
to marshal data as well as long as the COM component could be in a separate
process.

So there is no need to marshal data between threads. As any rules this rule
have exceptions. While normally data is not thread bound some windows
resources are (e.g. example windows handles are bound to threads). Or a
program itself can create thread bound data. This way there might be some
time needs to marshal data between threads as well.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

When invoking the Form's BeginInvoke method above, what is going on in
terms
of marshalling? Is the DataTable object being marshalled from the spawned
worker thread to the main thread?

The object that ContentServiceWrapper.GetChannelsTable returns is in the
memory of this process. Marshaling it is already done. As long as DataTable
is marshaled by value you have completely new copy created via serialization
of the original.
However bare in mind that there is marshaling only across process or
application domain boundaries. When you call form's BeginInvoke there is no
marshaling. If DataTable is thread dependant, as controls are for example
(to be honest I don't know), you need to get special precaution when you
using it in UpdateChannelGrid, becuase the object is created in different
thread. So in this case you need to do your own marshaling between threads.
..NET doesn't do anything for you in this case.
In terms of scheduling, is the thread
"scheduling an invocation of the UpdateChannelGrid method"?

UpdateChannelGrid is going to be scheduled for execution from the main UI
thread. The thread created the form. However because you use BeginInvoke
RetrieveChannelsCallback won't be block and will finish as soon as it calls
BeginInvoke.
 

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