473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ISynchronizeInv oke 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.Current Thread), 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 5103
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 ISynchronizeInv oke
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.co m

"HolyShea" <sh************ @gmail.comwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.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 ISynchronizeInv oke 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.Current Thread), 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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }

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

Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 Synchronization Context class...

In the constructor of the class, get a reference to the current
Synchronization Context

sc = Syncrhonization Context.Current ;

Then when I do my callback

if(IsConnectedE vent != null)
{
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t() // 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.guar d.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 ISynchronizeInv oke
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.c om

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

news:11******** **************@ n59g2000hsh.goo glegroups.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 ISynchronizeInv oke 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.Current Thread), 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 ISynchronizeInv oke. 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 ISynchronizeInv oke 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.co m

"HolyShea" <sh************ @gmail.comwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }

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

Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 Synchronization Context class...

In the constructor of the class, get a reference to the current
Synchronization Context

sc = Syncrhonization Context.Current ;

Then when I do my callback

if(IsConnectedE vent != null)
{
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t() // 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.guar d.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 ISynchronizeInv oke
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.c om

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

news:11******* *************** @n59g2000hsh.go oglegroups.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 ISynchronizeInv oke 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.Current Thread), 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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }

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

Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 ISynchronizeInv oke
object around.

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

sc = Synchronization Context.Current ;

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

sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t();
}), 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.guar d.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 ISynchronizeInv oke. 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 ISynchronizeInv oke 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.c om

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

news:11******** **************@ o5g2000hsb.goog legroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }
if (IsConnected())
{
if(IsConnectedE vent != null)
IsConnectedEven t() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 Synchronization Context class...
In the constructor of the class, get a reference to the current
Synchronization Context
sc = Syncrhonization Context.Current ;
Then when I do my callback
if(IsConnectedE vent != null)
{
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t() // 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.guar d.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 ISynchronizeInv oke
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.c om
"HolyShea" <shea.armstr... @gmail.comwrote in message
>news:11******* *************** @n59g2000hsh.go oglegroups.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 ISynchronizeInv oke 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.Current Thread), 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.co m

"HolyShea" <sh************ @gmail.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }

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

Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}

All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 ISynchronizeInv oke
object around.

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

sc = Synchronization Context.Current ;

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

sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t();
}), 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.guar d.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 ISynchronizeInv oke. 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 ISynchronizeInv oke 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.c om

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

news:11******* *************** @o5g2000hsb.goo glegroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }
if (IsConnected())
{
if(IsConnectedE vent != null)
IsConnectedEven t() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 Synchronization Context class...
In the constructor of the class, get a reference to the current
Synchronization Context
sc = Syncrhonization Context.Current ;
Then when I do my callback
if(IsConnectedE vent != null)
{
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t() // 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.guar d.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 ISynchronizeInv oke
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.c om
>"HolyShea" <shea.armstr... @gmail.comwrote in message
>>news:11****** *************** *@n59g2000hsh.g ooglegroups.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 ISynchronizeInv oke 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.Current Thread), 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 ISynchronizeInv oke through
the .target property of the delegate is an interesting idea...

The Synchronization Context 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.guar d.caspershouse. comwrote:
You just reposted the same response to my answer to this response?

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

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

news:11******** **************@ p77g2000hsh.goo glegroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }
if (IsConnected())
{
if(IsConnectedE vent != null)
IsConnectedEven t() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 ISynchronizeInv oke
object around.
*** Was just reading the measurement studio documentation and figured
out how they do it... you need to use the Synchronization Context
object. In the constructor of the class, keep a reference to
Synchronization Context.Current :
sc = Synchronization Context.Current ;
Then when you want to perform your callback onto the instantiating
thread:
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t();
}), 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.guar d.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 ISynchronizeInv oke. 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 ISynchronizeInv oke 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.c om
"HolyShea" <shea.armstr... @gmail.comwrote in message
>news:11******* *************** @o5g2000hsb.goo glegroups.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(!bConnecting Aync)
{
// Do this on a new thread
//
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(de legate
{
while (true)
{
try
{
dbConnection.Op en();
}
catch (InvalidOperati onException)
{ }
if (IsConnected())
{
if(IsConnectedE vent != null)
IsConnectedEven t() // Ideally, I'd like this to be
called on the thread that instantiated the class...
break;
}
Thread.Sleep(10 000);
}
bConnectingAync = false;
}));
}
}
All methods called by the IsConnectedEven t() 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 ISynchronizeInv oke 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 Synchronization Context class...
In the constructor of the class, get a reference to the current
Synchronization Context
sc = Syncrhonization Context.Current ;
Then when I do my callback
if(IsConnectedE vent != null)
{
sc.Send(new SendOrPostCallb ack(delegate
{
IsConnectedEven t() // 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.guar d.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 ISynchronizeInv oke
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.c om
"HolyShea" <shea.armstr... @gmail.comwrote in message
>news:11******* *************** @n59g2000hsh.go oglegroups.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 ISynchronizeInv oke 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.Current Thread), 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
3382
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 delegate call, the application is well behaved (no UI freeze), but the call to the com object causes the UI to lock up Do I have to manage calls...
4
3234
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, there is no pb and the program works fine. If either the proc is FENCED, or FENCED THREADSAFE, I get a SQL1042C, or the instance crashes on ZLinux...
20
3949
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 CInteropCall encapsulating a call to a visual basic ActiveX DLL function named FillAlloc_ArrayStruct_Double(), which is allocating a big struct array an...
0
1008
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 Windows 2000 Professional O.S. Whenever the required job is done in that background-running-thread, I would like to Enable and Start the Timer to do...
5
3239
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 btnUpload_Click(object sender, System.EventArgs e) { try { SOPWebService.Client uploader = new
55
10649
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
3789
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 property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom....
3
1630
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" message, and then when the script is done, fill the grids with this data. I've read deveral articles on multithreading, but I really haven't found how to...
7
2166
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 some-what. I am able to call form remotely and return data to client but somewhere after closing remote form and returning data - I get a Windows...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7959
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.