473,403 Members | 2,183 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,403 software developers and data experts.

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 5084
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marwan | last post by:
Hello I am using asynchronous delegates to make a call to a COM ActiveX object, but even though the call occurs on a separate thread, my UI is still blocking. If i put the thread to sleep in my...
4
by: Jean-Marc Blaise | last post by:
Dear all, I have simulated the windows MULTI application with a java program calling the SQLTP1DL proc referenced as DB2DARI application, on Linux Intel or ZLinux. If the proc is NOT FENCED,...
20
by: Cybertof | last post by:
Hello, Is there a good way to call a big time-consumming function from an ActiveX DLL (interoped) through a thread without blocking the main UI ? Here are the details : I have a class...
0
by: BilMuh | last post by:
Hello Esteemed Developers and Esteemed Experts, I have a background-running-thread in my application that is designed and developed at Windows Forms (.NET) by using Visual C++ .NET Standard on...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
55
by: salad | last post by:
I have contained in a listbox the Window's caption, the class name for the window, and the hWND of the window. Is there a way, using the data from above, to activate/set focus to that window?
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: VMI | last post by:
How can I use threading in my Windows Form to call a sql script that takes a few seconds to run? When I click on a button, I'd like to call the SP, display in my Form message a "Please wait"...
7
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am trying to see if I can call a Library remotely. The library contains a Form that I want to display then pass back some data to user that called this form remotely. I have it working...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.