473,698 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rising events between threads

Hi all,

I've recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I'm trying to use them in every weird way that comes
to my mind. Particularly I'm struggling to find a way to use events
between threads.

In particular I'd like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?

I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...

So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I'm not taling about Monitor, Mutex and
co... I know them...)?

Thanks in advance. Bye.

Jul 3 '07 #1
14 2834
You can not inject a call into the call stack of another thread without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that is,
have a loop waiting for a signal to execute your delegate.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Gotch@" <da******@gmail .comwrote in message
news:11******** **************@ n2g2000hse.goog legroups.com...
Hi all,

I've recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I'm trying to use them in every weird way that comes
to my mind. Particularly I'm struggling to find a way to use events
between threads.

In particular I'd like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?

I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...

So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I'm not taling about Monitor, Mutex and
co... I know them...)?

Thanks in advance. Bye.

Jul 3 '07 #2
Thanks,
and my question is... How do I do that?
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
You can not inject a call into the call stack of another thread without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that is,
have a loop waiting for a signal to execute your delegate.

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

"Gotch@" <dario...@gmail .comwrote in message

news:11******** **************@ n2g2000hse.goog legroups.com...
Hi all,
I've recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I'm trying to use them in every weird way that comes
to my mind. Particularly I'm struggling to find a way to use events
between threads.
In particular I'd like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?
I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...
So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I'm not taling about Monitor, Mutex and
co... I know them...)?
Thanks in advance. Bye.

Jul 3 '07 #3
Just like I told you, you basically have to have a loop in your thread
which will process messages/notifications which are set on other threads.
You can use windows messages, some sort of queue structure, etc, etc. It's
a basic producer/consumer pattern.

Is there a particular reason why you want to have one single thread
process delegates? What exactly are you trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Gotch@" <da******@gmail .comwrote in message
news:11******** **************@ m36g2000hse.goo glegroups.com.. .
Thanks,
and my question is... How do I do that?
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
> You can not inject a call into the call stack of another thread
without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread
has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that
is,
have a loop waiting for a signal to execute your delegate.

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

"Gotch@" <dario...@gmail .comwrote in message

news:11******* *************** @n2g2000hse.goo glegroups.com.. .
Hi all,
I've recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I'm trying to use them in every weird way that comes
to my mind. Particularly I'm struggling to find a way to use events
between threads.
In particular I'd like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?
I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...
So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I'm not taling about Monitor, Mutex and
co... I know them...)?
Thanks in advance. Bye.


Jul 3 '07 #4
It looks like what you want is System.Threadin g.ThreadPool. It keeps
a bunch of threads on standby and wakes them up to process delegates
for you. If you want a dedicated thread look at
System.Componen tModel.Backgrou ndWorker

Jul 3 '07 #5
On Tue, 03 Jul 2007 08:34:14 -0700, Gotch@ <da******@gmail .comwrote:
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
>[...] Because the UI thread has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that
is,
have a loop waiting for a signal to execute your delegate.

Thanks,
and my question is... How do I do that?
There are two obvious ways, depending on what the nature of the threads
are.

To pass a delegate to a UI thread, all you need to do is call Invoke() or
BeginInvoke() on a Control instance owned by that thread. Typically, this
would be the main form for the application.

To pass a delegate to a non-UI thread, you need to implement your own
version of an event/message queue that can deal with handling delegates.
And of course, the simplest version of that would be to simply have a
queue of delegates (e.g. "Queue<Delegate >"). If the delegate must always
conform to a specific signature, then use the specific delegate type
instead of "Delegate". If not, then you'll probably want to allow the
inclusion of an array of objects to pass as parameters as well (e.g. make
a struct that contains references to both a delegate instance and an
object[], and put that in your queue).

Keep in mind that you of course will want to synchronize access to the
queue, and you will want the processing thread (the one pulling things
from the queue to execute) to wait until some other thread has signaled
it. See the AutoResetEvent class and "lock()" statement for how to do
that.

Pete
Jul 3 '07 #6
On Tue, 03 Jul 2007 09:01:31 -0700, james <ja********@gma il.comwrote:
It looks like what you want is System.Threadin g.ThreadPool. It keeps
a bunch of threads on standby and wakes them up to process delegates
for you. If you want a dedicated thread look at
System.Componen tModel.Backgrou ndWorker
I don't think that's what he wants. It sounds as though he wants a
delegate to be run on a specific thread, not some arbitrary thread pulled
from the thread pool.

I could be wrong, but that's my reading of the thread so far.

Pete
Jul 3 '07 #7
Peter Duniho wrote:
On Tue, 03 Jul 2007 09:01:31 -0700, james <ja********@gma il.comwrote:
>It looks like what you want is System.Threadin g.ThreadPool. It keeps
a bunch of threads on standby and wakes them up to process delegates
for you. If you want a dedicated thread look at
System.Compone ntModel.Backgro undWorker

I don't think that's what he wants. It sounds as though he wants a
delegate to be run on a specific thread, not some arbitrary thread pulled
from the thread pool.

I could be wrong, but that's my reading of the thread so far.

Pete
Hi Pete,
I could be wrong, but that's my reading of the thread so far.
<big grin>

--
Tom Spink
University of Edinburgh
Jul 3 '07 #8
Gotch@ wrote:
Thanks,
and my question is... How do I do that?
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
> You can not inject a call into the call stack of another thread
without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread
has a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that
is,
have a loop waiting for a signal to execute your delegate.

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

"Gotch@" <dario...@gmail .comwrote in message

news:11******* *************** @n2g2000hse.goo glegroups.com.. .
Hi all,
I've recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I'm trying to use them in every weird way that comes
to my mind. Particularly I'm struggling to find a way to use events
between threads.
In particular I'd like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?
I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...
So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I'm not taling about Monitor, Mutex and
co... I know them...)?
Thanks in advance. Bye.
Hi,

I'm glad you're getting into C#. It is pretty superb.

Because what you said sounds really cool, I've whipped up a quick example of
executing a delegate on another thread, and I've made it deliberately
extensible to show that it uses a message-passing design pattern.

It would be rude to post it here, because it's a bit big, so please take a
look at it here:

http://www.betasafe.com/code?view=deleginv

--
Tom Spink
University of Edinburgh
Jul 3 '07 #9
On Tue, 03 Jul 2007 10:14:00 -0700, Tom Spink <ts****@gmail.c omwrote:
[...]
It would be rude to post it here, because it's a bit big, so please take
a
look at it here:

http://www.betasafe.com/code?view=deleginv
IMHO, it's rude _not_ to post the code here. Well, okay...maybe not so
much "rude" as "short-sighted".

This newsgroup is likely to be archived for some arbitrarily long period
of time. Who knows how long, but the current archives (the largest
repository I know of is now maintained by Google) go back decades. When
you post a link to a web site instead copying the code into the post
itself, you limit the lifetime of the usefulness of your post to that of
your web site. The message itself may live on longer than that (and it
probably will), but it won't be of any use to anyone else at that point.

When will your web site go defunct? One hopes not for a long time, but it
could be tomorrow, next month, next year, whatever. Whatever the
lifetime, it's too short as compared to the likely lifetime of the
newsgroup.

More importantly, if you include the code in your post, the lifetime of
that copy of the code is tied perfectly to the newsgroup, which is exactly
what one wants. Your article's lifetime should match the newsgroup's
lifetime, whether shorter or longer than your web site's, rather than
matching that of your web site.

Besides, while the code might be long, it's certainly no longer than the
huge number of top-posted, untrimmed articles other people post to the
newsgroup every day. I'm not saying two wrongs make a right, but in this
case I don't think it would be wrong to post the code, and in fact is
beneficial, and at the very least would help improve the signal-to-noise
ratio here (admittedly, already better than is found in many other
newsgroups, but still...)

Pete
Jul 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2249
by: GM | last post by:
Hi, My application has a need to cache a number of shared reference lists containing basic business objects. In order to improve performance these lists are fetched and updated in the background. When a UI is required to display one of these reference lists it requests it by name from the cache. If the list already exists in the cache it is returned to the caller otherwise a brand new empty one is created and returned. This new empty...
3
14293
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
7
4162
by: Raj Wall | last post by:
I have threads with event handlers to watch for new files placed in directories. The handler has a Using { ... } block to process the file which is then deleted. In testing I can drag/drop FileA into one of the watched directories and everything works fine. But when I drop FileB into one of the other directories (or the same one, it doesn't matter), I get a "File in Use" error. This seems odd since the second test is with a different file...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7737
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.