[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

How to build a Windows Forms remoting host?

gkustas

11/3/2004 1:48:00 AM

In the "old days", I could make a windows application an ActiveX exe,
and expose all the properties and methods I wanted. I could allow a
remote application to show and/or hide the application, or access
application data(i.e. current customer number in a customer order
application).

I now have a csharp windows forms app, and I'm trying to acheive the
same via remoting. I googled my brains out, looked at codeguru, etc.,
and I still can't find an item or example that does what I'm looking
for.

Lets say all I want to do is allow a remote client to make my main
application window visible or not visible...

1. I create a remotable class called Remote1, by inheriting
MarshalByRefObject.
2. I add a method to Remote1 called SetVisible(bool visible).
3. I create a windows form application with a class Form1, and a
Main().
4. In Main(), I know how to register my remotable class Remote1, and I
know how to instatiate it.
5. I also know how to write the client code to access the remotable
object.

But... How can my remotable class access my application's Form1 class
to make it visible or not?

An example here (or even an explanation) will help greatly... Thanks
5 Answers

Ken Kolda

11/3/2004 4:21:00 PM

0

When your server app starts up, it probably does something like the
following in its Main() routine:

Application.Run(new Form1());

What you can do is create a static field in the Form1 class to hold your
"main" form instance, e.g.

public class Form1 : System.Windows.Form
{
public static Form1 MainForm;

public static Main()
{
MainForm = new Form1();
Application.Run(MainForm);
}
}

Then, in your remoted class's SetVisible() function, just do the following:

public void SetVisible(bool visible)
{
if (Form1.MainForm.InvokeRequired)
Form1.MainForm.Invoke(new someDelegate(SetVisible), new object[] {
true });
else
Form1.MainForm.Visible = visible;
}

Hope that helps -
Ken


"George Kustas" <gkustas@hvc.rr.com> wrote in message
news:b4c52ba8.0411021748.555e69a5@posting.google.com...
> In the "old days", I could make a windows application an ActiveX exe,
> and expose all the properties and methods I wanted. I could allow a
> remote application to show and/or hide the application, or access
> application data(i.e. current customer number in a customer order
> application).
>
> I now have a csharp windows forms app, and I'm trying to acheive the
> same via remoting. I googled my brains out, looked at codeguru, etc.,
> and I still can't find an item or example that does what I'm looking
> for.
>
> Lets say all I want to do is allow a remote client to make my main
> application window visible or not visible...
>
> 1. I create a remotable class called Remote1, by inheriting
> MarshalByRefObject.
> 2. I add a method to Remote1 called SetVisible(bool visible).
> 3. I create a windows form application with a class Form1, and a
> Main().
> 4. In Main(), I know how to register my remotable class Remote1, and I
> know how to instatiate it.
> 5. I also know how to write the client code to access the remotable
> object.
>
> But... How can my remotable class access my application's Form1 class
> to make it visible or not?
>
> An example here (or even an explanation) will help greatly... Thanks


George Kustas

11/3/2004 8:58:00 PM

0


Maybe I'm missing something fundamental here, but as I understand it:

- My WindowsForms class can not be a remotable class, that is I can't
inherit frmo Windows.Forms and MarshalByRefObject, so...

- I create a new class and put it in a seperate class library so that
both the server and client can reference it.

I've got three projects:

1. My win forms application (the "server" in this case, call it
"AppServer"). It has a mainwindow class called "MainWindow".

2. My class library containing my remotable class (call it
"AppController")

3. Another WinForms application (call it "AppClient") that will use the
AppController to make AppServer visible or invisible

So, how can AppController reference the MainWindow class in "AppServer"?
I can only reference projects that have dll's as there target output.


*** Sent via Developersdex http://www.develop... ***
Don't just participate in USENET...get rewarded for it!

Ken Kolda

11/3/2004 9:39:00 PM

0

Actually, a form can be a remoting server -- the Windows.Forms.Form class
derived from MarshalByRefObject. That said, remoting a form is generally not
the best idea.

The way you can resolve your problem of referencing the form from the server
object is to use interface-based remoting. In this scenario, you create an
interface for your remote object and only that interface goes into the
shared class library. The actual implementation of the remote object goes
into your AppServer project.

When your client retrieves the object (e.g. via Activator.GetObject()), it
just gets an interface for the object through which it invokes methods.
Since the remote object lives in the AppServer project, it's free to
manipulate the main form as I suggested.

Here are a couple of good reference on interface-based remoting that shows
how you do this:

http://www.genuinechannels.com/Content.aspx?id=28&...
http://msdn.microsoft.com/architecture/patterns/default.aspx?pull=/library/en-us/dnpatterns/html/ImpBroker...

Ken


"George Kustas" <gkustas@contentsolutions.net> wrote in message
news:OmKi$eewEHA.3276@TK2MSFTNGP15.phx.gbl...
>
> Maybe I'm missing something fundamental here, but as I understand it:
>
> - My WindowsForms class can not be a remotable class, that is I can't
> inherit frmo Windows.Forms and MarshalByRefObject, so...
>
> - I create a new class and put it in a seperate class library so that
> both the server and client can reference it.
>
> I've got three projects:
>
> 1. My win forms application (the "server" in this case, call it
> "AppServer"). It has a mainwindow class called "MainWindow".
>
> 2. My class library containing my remotable class (call it
> "AppController")
>
> 3. Another WinForms application (call it "AppClient") that will use the
> AppController to make AppServer visible or invisible
>
> So, how can AppController reference the MainWindow class in "AppServer"?
> I can only reference projects that have dll's as there target output.
>
>
> *** Sent via Developersdex http://www.develop... ***
> Don't just participate in USENET...get rewarded for it!


George Kustas

11/4/2004 12:37:00 AM

0


Ken,

This seems to be just what I need - Thanks!

BTW... if I implement this in the AppServer, will I still be able to
fire events that the client can receive?


*** Sent via Developersdex http://www.develop... ***
Don't just participate in USENET...get rewarded for it!

Ken Kolda

11/4/2004 12:57:00 AM

0

Sure -- no problem. I generally recommend you stay away from built-in events
and, instead, use a listener registration model. What I mean by that is you
define an interface in your common library such as

public interface IEventListener
{
public OnServerEvent(...);
}

On your server object you would then have a method such as:

public void RegisterEventListener(IEventListener listener)
{
lock (someArrayList)
someArrayList.Add(listener);
}

On the client side, to create a listener, just create a
MarshalByRefObject-derived class and implement IEventListener. Then call
RegisterEventListener() on the server and pass the object in.

Finally, when you're ready to raise your event, just invoke all the
listeners individually. Make sure that when you invoke each listener that
you do so in a try/catch in case the client has disconnected. If you get an
exception, remove the listener from your ArrayList.

To me, this methodology provides the best client/server separation and the
cleanest implementation. That said, events will work as well, but you either
have to use an EventWrapper of some sort or your client objects'
implementation have to be available on the server.

Hope that helps -
Ken


"George Kustas" <gkustas@contentsolutions.net> wrote in message
news:OB4xcZgwEHA.4048@TK2MSFTNGP15.phx.gbl...
>
> Ken,
>
> This seems to be just what I need - Thanks!
>
> BTW... if I implement this in the AppServer, will I still be able to
> fire events that the client can receive?
>
>
> *** Sent via Developersdex http://www.develop... ***
> Don't just participate in USENET...get rewarded for it!