Ok, so i have got myself completely stuck. My search skill are lacking and I cant find anything that makes me see what I need to do.
What I have is a Telnet App. Form1 connects and preforms the socket stuff though a class. The class is refernced on form one like so
//form1
telnet tc;
private void connect_Click(object sender, EventArgs e)
{
tc = new telnet (IP, Port);
}
What I am trying to do is use this same method on form2. form2 is a configuration window and requires getting data from the telnet server to save to an xml file. via
//form 1
tc.read();
I could call a new instance of the telnet class but that would be a waste of resources and I think there’s a better way to code it.
So what I would like to know is, while in form2. How do I tell a button click event to access tc from form1?
So far I haven’t been able to figure out how to get the reference of form1 tc, or the connect_click button onto form 2.
//form2
private void read_click(object sender, EventArgs e)
{
tc.read(); // how do i call it? or even call a method of form 1 to button click?
... write data from tc.read()
}
One option is to create a singleton object that owns communications.
http://msdn.microsoft.com/en-us/library/ff650316.aspx
So, if you had something like:
Then, in your form functions you could just:
Since different forms are accessing this object, shouldn’t need to be thread safe. If it does, then you can use a double lock pattern.