By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

An Event Listener

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.