Marshall a call onto an instance of Thread

H

HolyShea

All,

Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?

I didn't see any method, either static or instance, in the Thread
class which provides this functionality.

If someone could shed some light on this I'd appreciate it.

Thanks,

Shea
 
N

Nicholas Paldino [.NET/C# MVP]

Shea,

If you are not doing some sort of looping in the thread that you are
making the calling from, you will not be able to do this. You can not just
inject code into a running thread. The reason why the Control class is able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.

Why can't you make this call on another thread? Can you give some more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?
 
H

HolyShea

Thanks for the response.

Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:

public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }

if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}

Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity than anything. In the past, to
solve this problem I've thrown an ISynchronizeInvoke instance all over
the place, but it's a bit of a nuisance. Just wondering if there was a
way to marshall a call onto a thread given it's .NET Thread object.

I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ... http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them.

** Actually, after reading their documentation, you can do what I want
by using the SynchronizationContext class...

In the constructor of the class, get a reference to the current
SynchronizationContext

sc = SyncrhonizationContext.Current;

Then when I do my callback

if(IsConnectedEvent != null)
{
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent() // Somehow gets marshalled onto the thread that
the constructor was called on
}), null);
}

Perhaps there are some caveats to this I'm not aware of? (I can't
think of anything but performance...)

Shea

Shea,

If you are not doing some sort of looping in the thread that you are
making the calling from, you will not be able to do this. You can not just
inject code into a running thread. The reason why the Control class is able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.

Why can't you make this call on another thread? Can you give some more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?
I didn't see any method, either static or instance, in the Thread
class which provides this functionality.
If someone could shed some light on this I'd appreciate it.

Shea
 
N

Nicholas Paldino [.NET/C# MVP]

Shea,

I doubt that the calls are marshaled automatically to the thread that
created them. What is more likely than not happening is that when the
callback is fired for the async operation, reflection is performed on the
object to determine if it implements ISynchronizeInvoke. If it does, then
the call is routed back to the thread that the call must be made on. I'm
assuming you want to do something similar. If you have a delegate, you can
work your way back to the ISynchronizeInvoke interface using the Target
property offered on the delegate.

There is no way to inject a call into the call stack of a running thread
with just the Thread object.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HolyShea said:
Thanks for the response.

Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:

public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }

if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}

Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity than anything. In the past, to
solve this problem I've thrown an ISynchronizeInvoke instance all over
the place, but it's a bit of a nuisance. Just wondering if there was a
way to marshall a call onto a thread given it's .NET Thread object.

I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ... http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them.

** Actually, after reading their documentation, you can do what I want
by using the SynchronizationContext class...

In the constructor of the class, get a reference to the current
SynchronizationContext

sc = SyncrhonizationContext.Current;

Then when I do my callback

if(IsConnectedEvent != null)
{
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent() // Somehow gets marshalled onto the thread that
the constructor was called on
}), null);
}

Perhaps there are some caveats to this I'm not aware of? (I can't
think of anything but performance...)

Shea

Shea,

If you are not doing some sort of looping in the thread that you are
making the calling from, you will not be able to do this. You can not
just
inject code into a running thread. The reason why the Control class is
able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent
into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.

Why can't you make this call on another thread? Can you give some
more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?
I didn't see any method, either static or instance, in the Thread
class which provides this functionality.
If someone could shed some light on this I'd appreciate it.

Shea
 
H

HolyShea

Thanks for the response.

Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:

public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }

if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}

Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity more than anything. In the past,
to solve this problem I've thrown an ISynchronizeInvoke instance all
over the place, but it's a bit of a nuisance. Just wondering if there
was a way to marshall a call onto a thread given it's .NET Thread
object.

I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ... http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them, without needing to pass an ISynchronizeInvoke
object around.

*** Was just reading the measurement studio documentation and figured
out how they do it... you need to use the SynchronizationContext
object. In the constructor of the class, keep a reference to
SynchronizationContext.Current:

sc = SynchronizationContext.Current;

Then when you want to perform your callback onto the instantiating
thread:

sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent();
}), null);

That seems to marshall it properly, I'm not sure how it does it, or if
there are any side effects of this approach, but it seems to work...

Shea


Shea,

I doubt that the calls are marshaled automatically to the thread that
created them. What is more likely than not happening is that when the
callback is fired for the async operation, reflection is performed on the
object to determine if it implements ISynchronizeInvoke. If it does, then
the call is routed back to the thread that the call must be made on. I'm
assuming you want to do something similar. If you have a delegate, you can
work your way back to the ISynchronizeInvoke interface using the Target
property offered on the delegate.

There is no way to inject a call into the call stack of a running thread
with just the Thread object.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks for the response.
Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:
public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }
if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity than anything. In the past, to
solve this problem I've thrown an ISynchronizeInvoke instance all over
the place, but it's a bit of a nuisance. Just wondering if there was a
way to marshall a call onto a thread given it's .NET Thread object.
I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ...http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them.
** Actually, after reading their documentation, you can do what I want
by using the SynchronizationContext class...
In the constructor of the class, get a reference to the current
SynchronizationContext
sc = SyncrhonizationContext.Current;
Then when I do my callback
if(IsConnectedEvent != null)
{
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent() // Somehow gets marshalled onto the thread that
the constructor was called on
}), null);
}
Perhaps there are some caveats to this I'm not aware of? (I can't
think of anything but performance...)
Shea,
If you are not doing some sort of looping in the thread that you are
making the calling from, you will not be able to do this. You can not
just
inject code into a running thread. The reason why the Control class is
able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent
into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.
Why can't you make this call on another thread? Can you give some
more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

All,
Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?
I didn't see any method, either static or instance, in the Thread
class which provides this functionality.
If someone could shed some light on this I'd appreciate it.
Thanks,
Shea
 
N

Nicholas Paldino [.NET/C# MVP]

You just reposted the same response to my answer to this response?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HolyShea said:
Thanks for the response.

Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:

public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }

if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}

Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity more than anything. In the past,
to solve this problem I've thrown an ISynchronizeInvoke instance all
over the place, but it's a bit of a nuisance. Just wondering if there
was a way to marshall a call onto a thread given it's .NET Thread
object.

I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ... http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them, without needing to pass an ISynchronizeInvoke
object around.

*** Was just reading the measurement studio documentation and figured
out how they do it... you need to use the SynchronizationContext
object. In the constructor of the class, keep a reference to
SynchronizationContext.Current:

sc = SynchronizationContext.Current;

Then when you want to perform your callback onto the instantiating
thread:

sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent();
}), null);

That seems to marshall it properly, I'm not sure how it does it, or if
there are any side effects of this approach, but it seems to work...

Shea


Shea,

I doubt that the calls are marshaled automatically to the thread that
created them. What is more likely than not happening is that when the
callback is fired for the async operation, reflection is performed on the
object to determine if it implements ISynchronizeInvoke. If it does,
then
the call is routed back to the thread that the call must be made on. I'm
assuming you want to do something similar. If you have a delegate, you
can
work your way back to the ISynchronizeInvoke interface using the Target
property offered on the delegate.

There is no way to inject a call into the call stack of a running
thread
with just the Thread object.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks for the response.
Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:
public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }
if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity than anything. In the past, to
solve this problem I've thrown an ISynchronizeInvoke instance all over
the place, but it's a bit of a nuisance. Just wondering if there was a
way to marshall a call onto a thread given it's .NET Thread object.
I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ...http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them.
** Actually, after reading their documentation, you can do what I want
by using the SynchronizationContext class...
In the constructor of the class, get a reference to the current
SynchronizationContext
sc = SyncrhonizationContext.Current;
Then when I do my callback
if(IsConnectedEvent != null)
{
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent() // Somehow gets marshalled onto the thread that
the constructor was called on
}), null);
}
Perhaps there are some caveats to this I'm not aware of? (I can't
think of anything but performance...)

On Apr 13, 1:47 pm, "Nicholas Paldino [.NET/C# MVP]"
Shea,
If you are not doing some sort of looping in the thread that you
are
making the calling from, you will not be able to do this. You can not
just
inject code into a running thread. The reason why the Control class
is
able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent
into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.
Why can't you make this call on another thread? Can you give some
more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?
Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when
the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do
this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?
I didn't see any method, either static or instance, in the Thread
class which provides this functionality.
If someone could shed some light on this I'd appreciate it.

Shea
 
H

HolyShea

Ugh, my response wasn't showing up for me... so I reposted on Sunday.
It's now there. Oops.

Anyhow, yeah, working your way back to the ISynchronizeInvoke through
the .target property of the delegate is an interesting idea...

The SynchronizationContext class seems to do exactly what I require
though, and doesn't require traversing through the .Target properties
of the delegates...

Shea

You just reposted the same response to my answer to this response?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Thanks for the response.
Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:
public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }
if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity more than anything. In the past,
to solve this problem I've thrown an ISynchronizeInvoke instance all
over the place, but it's a bit of a nuisance. Just wondering if there
was a way to marshall a call onto a thread given it's .NET Thread
object.
I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ...http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them, without needing to pass an ISynchronizeInvoke
object around.
*** Was just reading the measurement studio documentation and figured
out how they do it... you need to use the SynchronizationContext
object. In the constructor of the class, keep a reference to
SynchronizationContext.Current:
sc = SynchronizationContext.Current;
Then when you want to perform your callback onto the instantiating
thread:
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent();
}), null);
That seems to marshall it properly, I'm not sure how it does it, or if
there are any side effects of this approach, but it seems to work...
Shea,
I doubt that the calls are marshaled automatically to the thread that
created them. What is more likely than not happening is that when the
callback is fired for the async operation, reflection is performed on the
object to determine if it implements ISynchronizeInvoke. If it does,
then
the call is routed back to the thread that the call must be made on. I'm
assuming you want to do something similar. If you have a delegate, you
can
work your way back to the ISynchronizeInvoke interface using the Target
property offered on the delegate.
There is no way to inject a call into the call stack of a running
thread
with just the Thread object.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks for the response.
Let's say I wanted to connect to a database asynchronously... I could
write a class that included a method like this:
public void ConnectAsync()
{
if(!bConnectingAync)
{
// Do this on a new thread
//
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
{
while (true)
{
try
{
dbConnection.Open();
}
catch (InvalidOperationException)
{ }
if (IsConnected())
{
if(IsConnectedEvent != null)
IsConnectedEvent() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEvent() will be executed on a
thread other than that which they were instantiated. Performance is
not an issue in my application (one thread is fine), so I'd like to
not have to deal with any thread synchronization issues at all... it's
a matter of convenience & curiosity than anything. In the past, to
solve this problem I've thrown an ISynchronizeInvoke instance all over
the place, but it's a bit of a nuisance. Just wondering if there was a
way to marshall a call onto a thread given it's .NET Thread object.
I'm working with a 3rd party .NET library (National Instruments
Measurement Studio ...http://www.ni.com/mstudio) that does
asynchronous network communication, and the callbacks from the
"network" objects are automatically marshalled back to the thread
which instantiated them.
** Actually, after reading their documentation, you can do what I want
by using the SynchronizationContext class...
In the constructor of the class, get a reference to the current
SynchronizationContext
sc = SyncrhonizationContext.Current;
Then when I do my callback
if(IsConnectedEvent != null)
{
sc.Send(new SendOrPostCallback(delegate
{
IsConnectedEvent() // Somehow gets marshalled onto the thread that
the constructor was called on
}), null);
}
Perhaps there are some caveats to this I'm not aware of? (I can't
think of anything but performance...)
Shea
On Apr 13, 1:47 pm, "Nicholas Paldino [.NET/C# MVP]"
Shea,
If you are not doing some sort of looping in the thread that you
are
making the calling from, you will not be able to do this. You can not
just
inject code into a running thread. The reason why the Control class
is
able
to do this is that ultimately, the control is hosted on a thread that
continuously processes windows messages, and a windows message is sent
into
that loop to process the call to Invoke on the ISynchronizeInvoke
implementation.
Why can't you make this call on another thread? Can you give some
more
details about what it is you are trying to do? What is it that is
thread-specific that you have to maintain?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

All,
Not sure if this is possible or not - I've created a class which
performs an asynchronous operation and provides notification when
the
operation is complete. I'd like the notification to be performed on
the same thread thread that instantiated the class. One way to do
this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread (Thread.CurrentThread), store a
reference to that, and then somehow marshall the callback onto it?
I didn't see any method, either static or instance, in the Thread
class which provides this functionality.
If someone could shed some light on this I'd appreciate it.
Thanks,
Shea- Hide quoted text -

- Show quoted text -
 

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