473,573 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9148
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(o bject sender, EventArgs e)
{
Pipe<string> srvPipe = new Pipe<string>("p 1");
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(n ull); // Signal close to client pipe.
Console.WriteLi ne("Server done writing");
srvPipe.Close() ;
}

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

public Worker(string pipeName)
{
cPipe = Pipe<string>.Co nnect(pipeName, null);
t = new Thread(new ThreadStart(DoW ork));
t.Start();
}

private void DoWork()
{
Console.WriteLi ne("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(li ne);
}
sw.Flush();
}
cPipe.Close();
Console.WriteLi ne("Log closed.");
}
}
--
William Stacey [MVP]

"Steve Teeples" <St****@newsgro ups.nospam> wrote in message
news:E1******** *************** ***********@mic rosoft.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
20768
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? I seem to recall NS worked something like this: onmousedown=handleThat;
17
4864
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 event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')'...
2
10093
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 TcpListener(IPAddress.Any, 1234); Stream data = null; Socket socket = null;
0
2066
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 if it doesn't exist yet. The event manager uses weakrefs, so lists of listeners won't stop them
3
1965
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 event to happen. is it possible to accomplish this? thanks!
4
1146
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 self.listeners.keys(): #If the weakref has died, remove it and continue #through the list if listener is None:
2
4933
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 that connection and get the socket. I try to signal an event when this happens and pass that new socket object when the event happens, but when I try...
6
2656
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 enought to do using the preventDefault and stopPropagation event functions. IE's equivalent is supposed to be cancelBubble and returnValue,...
15
3758
by: Phillip B Oldham | last post by:
Are there any python event driven frameworks other than twisted?
0
7705
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
8206
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...
0
8077
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...
1
5601
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
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
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2224
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
1
1316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
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.