Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, March 4, 2016 7:39 PM
HI,
In my code bellow i want to call from ReturnListInfo class to form1 and execute methods in form1 without have to create other instant
Can anyone help please
namespace Client_APP2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
HttpChannel chn = new HttpChannel(7862);
ChannelServices.RegisterChannel(chn, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Form1),
"RemotingServer",
WellKnownObjectMode.Singleton);
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Connecting to Server.....\n\n");
IMachinePerformance mgr = (IMachinePerformance)Activator.GetObject(
typeof(IMachinePerformance),
"http://localhost:7861/RemotingServer");
listBox1.Items.Add(mgr.StartThread());
}
private void button2_Click(object sender, EventArgs e)
{
IMachinePerformance mgr = (IMachinePerformance)Activator.GetObject(
typeof(IMachinePerformance),
"http://localhost:7861/RemotingServer");
//try
//{
// mgr.EndThread();
//}
//catch (Exception err) { }
string cpu = mgr.GetCPU();
listBox1.Items.Add(cpu);
}
[Serializable]
public class sample_data
{
public string CPU { set; get; }
public string MEM { set; get; }
public TimeSpan datatime { get; set; }
}
public class ReturnListInfo : MarshalByRefObject, IMachiClient
{
public static void me(Form1 frm, string cpup)
{
frm.listBox1.Items.Add("add " + "__");
}
public _Sample_Data SendMachinePerformance(string _cpu, string _mem, TimeSpan _ts)
{
_Sample_Data _SD = new _Sample_Data();
_SD.CPU = _cpu;
_SD.MEM = _mem;
_SD._datatime = _ts;
Form1 frm = new Form1 ();
frm.listBox1.Items.Add(_cpu);
return _SD;
}
}
}
}
All replies (2)
Saturday, March 5, 2016 12:46 PM âś…Answered
You can get a reference to the already opened instance of Form1 from another class using the Application.OpenForms collection:
Form1 form1 = System.Windows.Forms.Application.OpenForms.OfType<Form1>().FirstOrDefault();
if (form1 != null)
{
form1.ListBox1.Items.Add("add " + "__");
}
You want to expose any fields that may be accessible from outside the Form class using properties:
Form1:
public ListBox ListBox1
{
get { return listBox1; }
}
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
Friday, March 4, 2016 10:53 PM
**In my code bellow i want to call from ReturnListInfo class to form1 and execute methods in form1 without have to create other instant **
You can do it with an Interface a contract between two classes. The Interface defines the public methods that are implemented by the form class.
The form class calls the other class passing the Interface implemented by the form class to the called class.
The called class can see the public methods on the form class via the Interface and execute the methods on the form class.