Hi. I have an instant messenger app. When a peer receives a message from another peer, his receive_message event is triggered and I'd like to spawn a new IM window. However, when I do so, it freezes.
This class raises the event
public class ClientProcess: MarshalByRefObject, IamShtoClient
{
//This event is used to notify us that we received a message.
public delegate void message( object sender, messageWrapper mw);
public event message messageReceived;
//Raise an event when we receive a message
[System.Runtime.Remoting.Messaging.OneWay]
void IamShtoClient.receiveMessage(string message, string senderUsername)
{
if(_debug)
log("received message: "+message+ " from: "+senderUsername);
messageReceived(this, new messageWrapper(message, senderUsername));
}
}
I add my event handler and implement it in a buddy list class
public class frmBuds : System.Windows.Forms.Form
{
private ClientProcess _shtoClient; //The remotable intermediary
public frmBuds(ClientProcess client)
{
// Required for Windows Form Designer support
InitializeComponent();
// Save the client reference
_shtoClient = client;
//hook up the received message event
_shtoClient.messageReceived +=
new ClientProcess.message(this.ClientProcess_MessageReceived);
}
private void ClientProcess_MessageReceived (
object sender, ClientProcess.messageWrapper mw)
{
frmIM IMForm = new frmIM(_shtoClient, mw.username, mw.message);
IMForm.Show();
}
}
The event structure seems to be working because I see my debug message and the IMForm starts appearing, which means the event handling works. However, as soon as the form starts appearing, it freezes... I am not sure why it freezes, perhaps I am not doing some initialization I'm using .net remoting for communication if that has something to do with it.
Please, any help would be very appreciated.

dynamically creating a form when receiving an event
mabrouk
The problem is problem is probably that New Message is received in a different thread from your main Thread so You need to Open the new Form in the Main form itself to solve this Issue!
Try to use a delegate to show the New form like this:
delegate void ShowFormDlgt(frmIM IMForm);
private void ShowForm(frmIM IMForm)
{
IMForm.Show();
}
And now modify your event like this:
private void ClientProcess_MessageReceived (object sender, ClientProcess.messageWrapper mw)
{
frmIM IMForm = new frmIM(_shtoClient, mw.username, mw.message);
this.Invoke(new ShowFormDlgt(ShowForm), IMForm);
}
I hope this will solve your problem!
Best Regards,
Santosh_DotNet
I do already keep track of the active windows between clients and have the helper method for passing messages. I just posted the required part of my code for people to understand my issue.
pcompassion
By the ways Show me the Updated code here which you changed after my suggestion!
Wish you best of Luck!
Best Regards,
zolivier
That part of the code looks like this now:
delegate void ShowFormDlgt(frmIM IMForm);
private void ShowForm(frmIM IMForm)
{
IMForm.Show();
}
private void ClientProcess_MessageReceived(
object sender, ClientProcess.messageWrapper mw)
{
IMForm = new frmIM(_shtoClient, mw.senderUsername, mw.message);
this.Invoke(new ShowFormDlgt(ShowForm), new object[] {IMForm} );
}
I also tried moving the delegate to ClientProcess class, but same thing happens.
jaapdevries
I would try creating a separate thread class to launch your message form. The thread would be defined outside of the scope of the event handler. Once the message is received, start that thread which will launch the IM form outside of the scope of the remoting client thread. You may want to add a method for passing a message to the open IM form, so if a new message comes in you can check whether the IM form is open and if it is you just invoke that method to pass the new message.
Jason Seel
cheekster