[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.remoting

Remoting event problem... client hangs after server fires event

me

8/19/2004 8:17:00 PM

I'm working on a "chat" application to learn DotNet remoting and I have
reviewed several examples, but I'm having a problem...

When the client is being initialized I instantiate the remote object (works
fine), then access a property on the server object to get the machine name
(works fine) , then call a method on the server object to add that machine
name to the list of clients (works fine), then the server fires an event
which is handled by the client (works fine), THEN at this point the client
just hands.... and the client "chat" form is never displayed.

I have three components (MyChatClient.exe, MyChatServer.exe, and
MyChatService.dll) and here is the pertinent information from each... Note:
I've snipped alot of code from the client and server in order to keep the
message short... I can post all the code if necessary....

----------------------------------------------------------------------------
MyChatServer --- just loads MyChatService
----------------------------------------------------------------------------

----------------------------------------------------------------------------
MyChatService
----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Diagnostics;

namespace MyChat
{
public delegate void UserAdded(string username);

public class ChatService : MarshalByRefObject
{
public event UserAdded evtUserAdded;

//array list of user who are currently logged into the chat
protected ArrayList Users;

//initialization
public ChatService()
{
Users = new System.Collections.ArrayList();
}

public override object InitializeLifetimeService()
{
return null;
}

public void AddUser(string name)
{
//add to Users list
Users.Add(name);
//fire this event
evtUserAdded(name);
}


public string GetServerName
{
get
{
return System.Windows.Forms.SystemInformation.ComputerName.ToString();
}
}

}
}

----------------------------------------------------------------------------
MyChatClient
----------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using MyChat;

public class Form1 : System.Windows.Forms.Form
{
//set reference to chat dll
public ChatService oChat;
private string m_MachineName;

public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();

//Connect remotely with config file
RemotingConfiguration.Configure("MyChatClient.exe.config");
oChat = new ChatService();


//get machine name for this client
m_MachineName = oChat.GetServerName;

//assign the event handlers for the ChatService events
oChat.evtUserAdded += new MyChat.UserAdded(this.UserAdded );

//inform server we have logged on
oChat.AddUser (m_MachineName);
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


public void UserAdded(string name)
{
UpdateUserText(name);
}

private void UpdateUserText(string str)
{
txtUsers.Clear();
txtUsers.AppendText((string)sb.ToString());
}
}

Again, the client app hangs after the "UserAdded" function runs on the
client... the Form is never displayed. If I stop and run the Client app
again I get the following error "Unable to connect to remote server".

Here is the pertinent info from the config files:
CLIENT CONFIG FILE
<client>
<wellknown type="MyChat.ChatService, MyChatService"
url="http://ieckvdgu902:9876/MyChatServer" />
</client>
<channels>
<channel ref="http" port="5555">
<serverProviders>
<formatter ref="soap" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>


SERVER CONFIG FILE
<service>
<wellknown mode="Singleton"
type="MyChat.ChatService, MyChatService"
objectUri="MyChatServer" />
</service>
<channels>
<channel ref="http" port="9876">
<serverProviders>
<formatter ref="soap" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>


Me

PS: I realize some of this code is not using "best practices" but I'm just
trying to learn something here.
2 Answers

Sam Santiago

8/20/2004 5:59:00 AM

0

You have a text box control, txtUsers. You are probably having threading
issues. You probably need to use the Control.Invoke method to update the
control with the thread that creating the control. Check out this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassinvok...

Thanks,

Sam

--
_______________________________
Sam Santiago
ssantiago@n0spam-SoftiTechture.com
http://www.SoftiTe...
_______________________________
"Me" <Me@discussions.microsoft.com> wrote in message
news:D33D76A5-66BC-4B49-9938-0F77A25AFD16@microsoft.com...
> I'm working on a "chat" application to learn DotNet remoting and I have
> reviewed several examples, but I'm having a problem...
>
> When the client is being initialized I instantiate the remote object
(works
> fine), then access a property on the server object to get the machine name
> (works fine) , then call a method on the server object to add that machine
> name to the list of clients (works fine), then the server fires an event
> which is handled by the client (works fine), THEN at this point the client
> just hands.... and the client "chat" form is never displayed.
>
> I have three components (MyChatClient.exe, MyChatServer.exe, and
> MyChatService.dll) and here is the pertinent information from each...
Note:
> I've snipped alot of code from the client and server in order to keep the
> message short... I can post all the code if necessary....
>
> --------------------------------------------------------------------------
--
> MyChatServer --- just loads MyChatService
> --------------------------------------------------------------------------
--
>
> --------------------------------------------------------------------------
--
> MyChatService
> --------------------------------------------------------------------------
--
> using System;
> using System.Collections;
> using System.Diagnostics;
>
> namespace MyChat
> {
> public delegate void UserAdded(string username);
>
> public class ChatService : MarshalByRefObject
> {
> public event UserAdded evtUserAdded;
>
> //array list of user who are currently logged into the chat
> protected ArrayList Users;
>
> //initialization
> public ChatService()
> {
> Users = new System.Collections.ArrayList();
> }
>
> public override object InitializeLifetimeService()
> {
> return null;
> }
>
> public void AddUser(string name)
> {
> //add to Users list
> Users.Add(name);
> //fire this event
> evtUserAdded(name);
> }
>
>
> public string GetServerName
> {
> get
> {
> return System.Windows.Forms.SystemInformation.ComputerName.ToString();
> }
> }
>
> }
> }
>
> --------------------------------------------------------------------------
--
> MyChatClient
> --------------------------------------------------------------------------
--
>
> using System;
> using System.Drawing;
> using System.Collections;
> using System.ComponentModel;
> using System.Windows.Forms;
> using System.Data;
> using System.Text;
> using System.Threading;
> using System.Runtime.Remoting;
> using System.Runtime.Remoting.Channels;
> using System.Runtime.Remoting.Channels.Http;
> using MyChat;
>
> public class Form1 : System.Windows.Forms.Form
> {
> //set reference to chat dll
> public ChatService oChat;
> private string m_MachineName;
>
> public Form1()
> {
> // Required for Windows Form Designer support
> InitializeComponent();
>
> //Connect remotely with config file
> RemotingConfiguration.Configure("MyChatClient.exe.config");
> oChat = new ChatService();
>
>
> //get machine name for this client
> m_MachineName = oChat.GetServerName;
>
> //assign the event handlers for the ChatService events
> oChat.evtUserAdded += new MyChat.UserAdded(this.UserAdded );
>
> //inform server we have logged on
> oChat.AddUser (m_MachineName);
> }
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }
>
>
> public void UserAdded(string name)
> {
> UpdateUserText(name);
> }
>
> private void UpdateUserText(string str)
> {
> txtUsers.Clear();
> txtUsers.AppendText((string)sb.ToString());
> }
> }
>
> Again, the client app hangs after the "UserAdded" function runs on the
> client... the Form is never displayed. If I stop and run the Client app
> again I get the following error "Unable to connect to remote server".
>
> Here is the pertinent info from the config files:
> CLIENT CONFIG FILE
> <client>
> <wellknown type="MyChat.ChatService, MyChatService"
> url="http://ieckvdgu902:9876/MyChatServer" />
> </client>
> <channels>
> <channel ref="http" port="5555">
> <serverProviders>
> <formatter ref="soap" typeFilterLevel="Full" />
> </serverProviders>
> </channel>
> </channels>
>
>
> SERVER CONFIG FILE
> <service>
> <wellknown mode="Singleton"
> type="MyChat.ChatService, MyChatService"
> objectUri="MyChatServer" />
> </service>
> <channels>
> <channel ref="http" port="9876">
> <serverProviders>
> <formatter ref="soap" typeFilterLevel="Full" />
> </serverProviders>
> </channel>
> </channels>
>
>
> Me
>
> PS: I realize some of this code is not using "best practices" but I'm
just
> trying to learn something here.


me

8/20/2004 3:11:00 PM

0

Thanks for the reply Sam!

I realized that it wasn't really a Remoting problem at all.... During the
initialization of the Client App, the form isn't yet loaded, so none of the
Controls on the form are available during initialization. Calling the
"AddUser" method on the server caused the "UserAdded" event on the client to
fire before the Form was loaded, which was causing the error. I added
conditional code in the "UserAdded" event on the client to see if the form is
loaded, and act accordingly. If the form is not yet loaded, store the data
and update the txtUsers control in the Form_Load event... if it's already
loaded, just go ahead and update the control in the event handler.