Call Form from Library thru remoting

G

Guest

Hi

I am trying to see if I can call a Library remotely. The library contains a
Form
that I want to display then pass back some data to user that called this
form remotely.

I have it working some-what.
I am able to call form remotely and return data to client but somewhere
after closing remote form and returning data - I get a Windows exception -
not sure where though

If anyone can see anything it would be greatly appreciated or better ways of
doing this ???

Thanks

Server form
=====

public Form1()
{
InitializeComponent();
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);

// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyObject),
"HelloWorld", WellKnownObjectMode.SingleCall);
}



Client form
====

MyObject obj;

public Form1()
{
InitializeComponent();

// Create a channel for communicating with the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);

// Create an instance of the remote object
obj = (MyObject)Activator.GetObject(typeof(MyRemoting.MyObject),
"tcp://localhost:8080/HelloWorld");

}

private void simpleButton1_Click(object sender, EventArgs e)
{


// Use the object
if( obj.Equals(null) )
{
this.Text = "Error: unable to locate server";
}
else
{
this.Text = obj.HelloWorld();
}

}



Library - I have public property on Form that captures text in textbox
====

public class MyObject : MarshalByRefObject
{
public MyObject()
{
}
/// Return a hello message
public string HelloWorld()
{
//return "Hello World!";

Mathfrm frm = new Mathfrm();
frm.ShowDialog();
return (frm.Message);
}


}
 
N

Nicholas Paldino [.NET/C# MVP]

sippyuconn,

Chances are here that the exception is occuring because when you make
the call to ShowDialog, it is on a thread other than the main UI thread.
When the call is remoted to the server form, the call comes in on another
thread, and you are trying to make a call to ShowDialog, which has to be
done on the main UI thread.

You can call Invoke, passing a delegate which will make the call to
ShowDialog.

I suspect this is just a test to learn remoting, but you should be aware
that this brings up another issue though. If you have multiple clients
calling this form, then it is possible that more than one dialog will be
shown at the same time.

Of course, another issue is that this requires the program to be running
on a logged in user session on the server side. This might be desirable, it
might not be, but it's something to be aware of.
 
G

Guest

Hi Nicholas

Yes - I am just trying to get it running thru a test pgm

Do you have an example of using Invoke to pass a Delegate to make the call???

On using remoting - since I wanted to popup a UI I assume I wouldn't use
this as
a true Client/Server. I would assume this would run on the client but have the
remoted pgm on a shared drive - and the UI run in the pgm space of the
client pc. This way it could be pushed to all the clients by installing once
to the shared drive. We wouldn't want to have a UI exposed on a true
client/server - since the server would be un-attended. The older version of
this pgm was on c++ as a exe that was called cmdline with no gui(in the newer
version UI i integral to the pgm).
Clients would run on mapped drives and also true client/server installs
since no gui was involved. Since the UI is now in play that has limited my
options
a)remoting but only with mapped drive - no true client/server
b)somehow expose a COM interface to gui
c)do another cmd line interface to pgm

thanks
Nicholas Paldino said:
sippyuconn,

Chances are here that the exception is occuring because when you make
the call to ShowDialog, it is on a thread other than the main UI thread.
When the call is remoted to the server form, the call comes in on another
thread, and you are trying to make a call to ShowDialog, which has to be
done on the main UI thread.

You can call Invoke, passing a delegate which will make the call to
ShowDialog.

I suspect this is just a test to learn remoting, but you should be aware
that this brings up another issue though. If you have multiple clients
calling this form, then it is possible that more than one dialog will be
shown at the same time.

Of course, another issue is that this requires the program to be running
on a logged in user session on the server side. This might be desirable, it
might not be, but it's something to be aware of.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

sippyuconn said:
Hi

I am trying to see if I can call a Library remotely. The library contains
a
Form
that I want to display then pass back some data to user that called this
form remotely.

I have it working some-what.
I am able to call form remotely and return data to client but somewhere
after closing remote form and returning data - I get a Windows exception -
not sure where though

If anyone can see anything it would be greatly appreciated or better ways
of
doing this ???

Thanks

Server form
=====

public Form1()
{
InitializeComponent();
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);

// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyObject),
"HelloWorld", WellKnownObjectMode.SingleCall);
}



Client form
====

MyObject obj;

public Form1()
{
InitializeComponent();

// Create a channel for communicating with the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);

// Create an instance of the remote object
obj =
(MyObject)Activator.GetObject(typeof(MyRemoting.MyObject),
"tcp://localhost:8080/HelloWorld");

}

private void simpleButton1_Click(object sender, EventArgs e)
{


// Use the object
if( obj.Equals(null) )
{
this.Text = "Error: unable to locate server";
}
else
{
this.Text = obj.HelloWorld();
}

}



Library - I have public property on Form that captures text in textbox
====

public class MyObject : MarshalByRefObject
{
public MyObject()
{
}
/// Return a hello message
public string HelloWorld()
{
//return "Hello World!";

Mathfrm frm = new Mathfrm();
frm.ShowDialog();
return (frm.Message);
}


}
 
L

Linda Liu [MSFT]

Hi,
Do you have an example of using Invoke to pass a Delegate to make the
call???

The following is a sampe:

delegate void MethodCallback();

public class MyObject : MarshalByRefObject
{
/// Return a hello message
public string HelloWorld()
{
if(this.InvokeRequired)
{
MethodCallback callback = new MethodCallback(HelloWorld);
string result = this.Invoke(callback).ToString();
return result;
}
else
{
Mathfrm frm = new Mathfrm();
frm.ShowDialog();
return (frm.Message);
}
}
}

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Sippyuconn,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Hi Linda - Thanks for the code but ....

I wasn't able to compile. The code where you added the Invoke and
InvokeRequired
is a not a windows form. Is there a way around that ????

Thanks
 
L

Linda Liu [MSFT]

Hi,

Thank you for your reply!
The code where you added the Invoke and InvokeRequired is a not a windows
form. Is there a way around that ????

You should add a reference to the assembly 'System.Windows.Forms' in the
class library project.

Hope this helps.
If you have any qustion, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hello,

How about the problem now?

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top