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

An Event Listener

I have been perplexed by how to best treat an event that spans different
classes.

For example, I have a form which a user inputs data. I want to broadcast
that data via an event to another class (seen globally) having a data
structure which saves that form data to disk.

Whenever the form updates the data I'd like to broadcast the information and
have it saved in my global data structure. The perplexing thing for me
though is the "listener" object. The listener is instanciated in the class
containing the global data structure which saves to disk. However, I don't
have the "requestor" instanciated here. It is instanciated in the form where
the data is updated. But, the "listener" needs as a parameter input the
"requestor". How to I use events to send data from one form to another when
the listener and requester don't have scope to each other?

--
-----------
Thanks,
Steve
Feb 7 '06 #1
6 9111
Steve,
At first blush it seems like all you really need to do is create an event in
the class where the user enters data, and have the other classes that need to
be notified subscribe to this event.

A .NET event can have multiple subscribers.

This is easily done with event delegates.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Steve Teeples" wrote:
I have been perplexed by how to best treat an event that spans different
classes.

For example, I have a form which a user inputs data. I want to broadcast
that data via an event to another class (seen globally) having a data
structure which saves that form data to disk.

Whenever the form updates the data I'd like to broadcast the information and
have it saved in my global data structure. The perplexing thing for me
though is the "listener" object. The listener is instanciated in the class
containing the global data structure which saves to disk. However, I don't
have the "requestor" instanciated here. It is instanciated in the form where
the data is updated. But, the "listener" needs as a parameter input the
"requestor". How to I use events to send data from one form to another when
the listener and requester don't have scope to each other?

--
-----------
Thanks,
Steve

Feb 7 '06 #2
Peter,

All the examples I've seen show the requester and listener being
instanciated within the same method. I have a case where a class - when
instanciated - will create the requestor. I have a global data structure
that has a listener. The problem I have is that the listener requires as a
parameter a reference to the requester. Does the requester instanciation
need to have a static method then for my listener to reference it?
--
-----------
Thanks,
Steve
"Peter Bromberg [C# MVP]" wrote:
Steve,
At first blush it seems like all you really need to do is create an event in
the class where the user enters data, and have the other classes that need to
be notified subscribe to this event.

A .NET event can have multiple subscribers.

This is easily done with event delegates.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Steve Teeples" wrote:
I have been perplexed by how to best treat an event that spans different
classes.

For example, I have a form which a user inputs data. I want to broadcast
that data via an event to another class (seen globally) having a data
structure which saves that form data to disk.

Whenever the form updates the data I'd like to broadcast the information and
have it saved in my global data structure. The perplexing thing for me
though is the "listener" object. The listener is instanciated in the class
containing the global data structure which saves to disk. However, I don't
have the "requestor" instanciated here. It is instanciated in the form where
the data is updated. But, the "listener" needs as a parameter input the
"requestor". How to I use events to send data from one form to another when
the listener and requester don't have scope to each other?

--
-----------
Thanks,
Steve

Feb 7 '06 #3
Hi Steve,

Thanks for your post.

I am not sure if I understand your problem very well. Based on my
understanding, the "requestor" is the one who fires the updating event,
while the "listener" is the one who wants to register the event and gets
the notification. The current problem is how to get a reference of the
"requestor" from "listener" side. If I misunderstand you, please feel free
to tell me. Thanks

Regarding this, the ways to obtain the "requestor" reference from
"listener" varies based on the contexts. The "requestor" and "listener"
must have some relation, for example, the most common way is that: Form
created the instance of "listener", and then Form can pass "requestor"
reference as a parameter of "listener" constructor.

Also, if certain intermediate class(such as Form) both have the "requestor"
and "listener" references, it can invoke certain method of "listener" to
pass in the "requestor" reference as a parameter.

Once getting the "requestor" reference, we can easily register the event
without any problem.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 8 '06 #4
You could try my Pipe<T> class. Form1 (class) could create the server side
of the pipe and class2 could have a thread that blocks on client side of
Pipe. When class1 sends object to the pipe, class2 just removes it and does
something with it. Pipe is created by "name" and global to AppDomain. So
your class2 can just connect to it by name if it exists (like a managed
NamedPipe, but local to your AppDomain)
http://channel9.msdn.com/ShowPost.aspx?PostID=161030

Here is a simple example:

private void button2_Click(object sender, EventArgs e)
{
Pipe<string> srvPipe = new Pipe<string>("p1");
Worker w = new Worker("p1"); // Create and start your
worker class.

// Write to pipe with some data.
for (int i = 0; i < 50; i++)
{
srvPipe.Write("Line" + i);
}
srvPipe.Write(null); // Signal close to client pipe.
Console.WriteLine("Server done writing");
srvPipe.Close();
}

public class Worker
{
private Thread t;
private Pipe<string> cPipe;

public Worker(string pipeName)
{
cPipe = Pipe<string>.Connect(pipeName, null);
t = new Thread(new ThreadStart(DoWork));
t.Start();
}

private void DoWork()
{
Console.WriteLine("Worker started.");
using (FileStream fs = File.Open(@"c:\log.txt", FileMode.Append))
using (StreamWriter sw = new StreamWriter(fs))
{
while (true)
{
string line = cPipe.Read();
if (line == null)
break;
sw.WriteLine(line);
}
sw.Flush();
}
cPipe.Close();
Console.WriteLine("Log closed.");
}
}
--
William Stacey [MVP]

"Steve Teeples" <St****@newsgroups.nospam> wrote in message
news:E1**********************************@microsof t.com...
|I have been perplexed by how to best treat an event that spans different
| classes.
|
| For example, I have a form which a user inputs data. I want to broadcast
| that data via an event to another class (seen globally) having a data
| structure which saves that form data to disk.
|
| Whenever the form updates the data I'd like to broadcast the information
and
| have it saved in my global data structure. The perplexing thing for me
| though is the "listener" object. The listener is instanciated in the
class
| containing the global data structure which saves to disk. However, I
don't
| have the "requestor" instanciated here. It is instanciated in the form
where
| the data is updated. But, the "listener" needs as a parameter input the
| "requestor". How to I use events to send data from one form to another
when
| the listener and requester don't have scope to each other?
|
| --
| -----------
| Thanks,
| Steve
Feb 8 '06 #5
I appreciate everyone's suggestions - they've been a big help. After reading
your suggestings I thought a lot about this and now understand how to
accomplish what I'm trying to do. Thanks.
--
-----------
Thanks,
Steve
""Jeffrey Tan[MSFT]"" wrote:
Hi Steve,

Thanks for your post.

I am not sure if I understand your problem very well. Based on my
understanding, the "requestor" is the one who fires the updating event,
while the "listener" is the one who wants to register the event and gets
the notification. The current problem is how to get a reference of the
"requestor" from "listener" side. If I misunderstand you, please feel free
to tell me. Thanks

Regarding this, the ways to obtain the "requestor" reference from
"listener" varies based on the contexts. The "requestor" and "listener"
must have some relation, for example, the most common way is that: Form
created the instance of "listener", and then Form can pass "requestor"
reference as a parameter of "listener" constructor.

Also, if certain intermediate class(such as Form) both have the "requestor"
and "listener" references, it can invoke certain method of "listener" to
pass in the "requestor" reference as a parameter.

Once getting the "requestor" reference, we can easily register the event
without any problem.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 8 '06 #6
Hi Steve,

I am glad our replies can help you. If you need further help, please feel
free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 9 '06 #7

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

Similar topics

5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
2
by: Craig | last post by:
Hi I listen on a port, when data is received I raise an event (OnMessageReceived) in the while loop as follows: private void WaitForConnection() { TcpListener listener = new...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
3
by: sowencheung | last post by:
I attach an Event in document.onkeyup and another event in document.onbeforeunload in onkeyup event, if the condition is satisfied, i will activate sth, then i don't want the onbeforeunload...
4
by: steve | last post by:
The example code from: http://sjbrown.ezide.com/games/example1.py.html .... def Notify( self, event ): if not isinstance(event, TickEvent): Debug( " Message: " + event.name ) for listener in...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
6
by: blaine | last post by:
Hello, I'm currently overriding function keys (F1 to F4) to perform other actions. In order to do this the default popup windows of Help (F1), Seach(F3) etc must be turned off. In FF it's easy...
15
by: Phillip B Oldham | last post by:
Are there any python event driven frameworks other than twisted?
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.