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