Showing Form using .NET Remoting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Form class that can be open by parent Form or by .NET Remoting
command.
When the parent Form opens the Form - All fine !
But when the .NET Remoting command is trying to Show the Form, the Form
stuck !! I tried to change the Remoting delegate invocation to asynchronous
and to synchronous, yet both cause the same Form Show() to stuck.
When I mean stuck, I mean the the Form is shown but non of it controls are
shown, only gray window that does not respond.

Both the parent Form and the Remoting command are invoking the same method
to Showing the Form, here is the code:

public void OpenViewForm(bool topMost)
{
m_Viewer = new ViewerForm();
m_Viewer.TopMost = topMost;
m_Viewer.Show();
m_Viewer.BringToFront();
}

Any idea why the remoting cause this and how can I fix this?
 
Ok, I found the problem:

The remoting method is being called on a seperate thread to the main thread
- therefore it has no message loop.

The remoting class must have to have access to one of the elements of the UI
(e.g. the main form).

Then calling a method on this form using Invoke, and bringing up of the form
in there.

i.e.

public class MainForm : Form
{
static MainForm m_formCurrent = null;

static public Current
{
get
{
return m_form;
}
}

private Viewer m_Viewer;

public MainForm()
{
m_formCurrent = this;
// etc etc
}

public void OpenViewForm()
{
m_Viewer = new ViewerForm();
m_Viewer.Show();
m_Viewer.BringToFront();
}
}

// in theremoting class
MainForm.Current.Invoke(new MethodInvoker(MainForm.Current.OpenViewForm));

All calls to the viewer should be marshalled like this : by using delegates.
 
Thats because the remoting call is coming in on a threadpool thread which does not run a message pump.

In your Main of the remote process do something like

Form1 f ;
delegate void ShowFormDel();

static void Main()
{
f = new Form1();
RemotingConfiguration.Configure(...);
Application.Run();
}

Then in the remoted call say
f.BeginInvoke(new ShowFormDel(f.Show), new object[]{});

Now this will marshall the call to show on to the UI thread which is running a message pump and so will show the form successfully.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have a Form class that can be open by parent Form or by .NET Remoting
command.
When the parent Form opens the Form - All fine !
But when the .NET Remoting command is trying to Show the Form, the Form
stuck !! I tried to change the Remoting delegate invocation to asynchronous
and to synchronous, yet both cause the same Form Show() to stuck.
When I mean stuck, I mean the the Form is shown but non of it controls are
shown, only gray window that does not respond.

Both the parent Form and the Remoting command are invoking the same method
to Showing the Form, here is the code:

public void OpenViewForm(bool topMost)
{
m_Viewer = new ViewerForm();
m_Viewer.TopMost = topMost;
m_Viewer.Show();
m_Viewer.BringToFront();
}

Any idea why the remoting cause this and how can I fix this?
 
Back
Top