469,644 Members | 1,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Marshall a call onto an instance of Thread

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

Apr 13 '07 #1
6 4497
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]
- mv*@spam.guard.caspershouse.com

"HolyShea" <sh************@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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

Apr 13 '07 #2
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]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"HolyShea" <shea.armstr...@gmail.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
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

Apr 13 '07 #3
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]
- mv*@spam.guard.caspershouse.com

"HolyShea" <sh************@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"HolyShea" <shea.armstr...@gmail.comwrote in message

news:11**********************@n59g2000hsh.googleg roups.com...
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


Apr 13 '07 #4
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
On Apr 13, 6:27 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"HolyShea" <shea.armstr...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
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]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
"HolyShea" <shea.armstr...@gmail.comwrote in message
>news:11**********************@n59g2000hsh.googleg roups.com...
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

Apr 15 '07 #5
You just reposted the same response to my answer to this response?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"HolyShea" <sh************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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
On Apr 13, 6:27 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"HolyShea" <shea.armstr...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegr oups.com...
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]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
>"HolyShea" <shea.armstr...@gmail.comwrote in message
>>news:11**********************@n59g2000hsh.google groups.com...
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


Apr 16 '07 #6
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

On Apr 16, 1:16 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
You just reposted the same response to my answer to this response?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"HolyShea" <shea.armstr...@gmail.comwrote in message

news:11**********************@p77g2000hsh.googlegr oups.com...
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
On Apr 13, 6:27 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
"HolyShea" <shea.armstr...@gmail.comwrote in message
>news:11**********************@o5g2000hsb.googlegr oups.com...
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]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
"HolyShea" <shea.armstr...@gmail.comwrote in message
>news:11**********************@n59g2000hsh.googleg roups.com...
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 -

Apr 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jean-Marc Blaise | last post: by
20 posts views Thread by Cybertof | last post: by
reply views Thread by BilMuh | last post: by
5 posts views Thread by Paul Hasell | last post: by
46 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by VMI | last post: by
7 posts views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.