Subject: Problems with threads
What is the problem in this example?
I have a class named Client that sends a message and receives another.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Matrix.Xmpp;
using Matrix.Xmpp.Client;
using System.Threading;
namespace XMPPApplication
{
class Client
{
private XmppClient xmppClient;
private List<string> listMessages;
private string lic;
public Client(string user, string pass, string sendTo, string message,string lic)
{
listMessages = new List<string>();
this.lic = lic;
xmppClient = new XmppClient
{
XmppDomain = "jbgomez-pc",
Username = user,
Password = pass
};
xmppClient.OnRosterEnd += delegate { this.SendMessageOnRosterEnd(sendTo, message); };
xmppClient.OnMessage += this.xmppClient_OnMessage;
}
void xmppClient_OnMessage(object sender, MessageEventArgs e)
{
Console.WriteLine("Soy el hilo de xmppClient_OnMessage: " + Thread.CurrentThread.ManagedThreadId + " " + e.Message.Body);
listMessages.Add(e.Message.Body);
}
public void SendMessageOnRosterEnd(string sendTo, string message)
{
Console.WriteLine("Soy el hilo de SendMessageOnRosterEnd: " + Thread.CurrentThread.ManagedThreadId + " " + message);
xmppClient.Send(new Message
{
To = sendTo,
Type = MessageType.chat,
Body = message
});
}
public void Open()
{
Matrix.License.LicenseManager.SetLicense(this.lic);
xmppClient.Open();
}
public void Close()
{
xmppClient.Close();
}
public string GetMessage()
{
string msg = null;
if (listMessages.Count > 0)
{
msg = listMessages[0];
listMessages.Remove(msg);
}
return msg;
}
}
}
The Main program is the next:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace XMPPApplication
{
class Program
{
static void Main(string[] args)
{
Client c1 = new Client("jbgomez", "xxxxxx", "jbgomez2@jbgomez-pc", "Soy el cliente 1", License.lic1);
Client c2 = new Client("jbgomez2", "xxxxxx", "jbgomez@jbgomez-pc", "Soy el cliente 2", License.lic1);
c1.Open();
c2.Open();
//sleep 30 sec
Thread.Sleep(30000);
Console.WriteLine(c1.GetMessage());
Console.WriteLine(c2.GetMessage());
c1.Close();
c2.Close();
Console.ReadLine();
}
}
}
When I run program only it execute one event, I get on the console:
Soy el hilo de SendMessageOnRosterEnd: 9 Soy el cliente 1
It should run four events....
Two SendMessageOnRosterEnd and two xmppClient_OnMessage
Why this events doesn't run?
I'm sorry for my bad english
Regards,
Juan
I have a class named Client that sends a message and receives another.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Matrix.Xmpp;
using Matrix.Xmpp.Client;
using System.Threading;
namespace XMPPApplication
{
class Client
{
private XmppClient xmppClient;
private List<string> listMessages;
private string lic;
public Client(string user, string pass, string sendTo, string message,string lic)
{
listMessages = new List<string>();
this.lic = lic;
xmppClient = new XmppClient
{
XmppDomain = "jbgomez-pc",
Username = user,
Password = pass
};
xmppClient.OnRosterEnd += delegate { this.SendMessageOnRosterEnd(sendTo, message); };
xmppClient.OnMessage += this.xmppClient_OnMessage;
}
void xmppClient_OnMessage(object sender, MessageEventArgs e)
{
Console.WriteLine("Soy el hilo de xmppClient_OnMessage: " + Thread.CurrentThread.ManagedThreadId + " " + e.Message.Body);
listMessages.Add(e.Message.Body);
}
public void SendMessageOnRosterEnd(string sendTo, string message)
{
Console.WriteLine("Soy el hilo de SendMessageOnRosterEnd: " + Thread.CurrentThread.ManagedThreadId + " " + message);
xmppClient.Send(new Message
{
To = sendTo,
Type = MessageType.chat,
Body = message
});
}
public void Open()
{
Matrix.License.LicenseManager.SetLicense(this.lic);
xmppClient.Open();
}
public void Close()
{
xmppClient.Close();
}
public string GetMessage()
{
string msg = null;
if (listMessages.Count > 0)
{
msg = listMessages[0];
listMessages.Remove(msg);
}
return msg;
}
}
}
The Main program is the next:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace XMPPApplication
{
class Program
{
static void Main(string[] args)
{
Client c1 = new Client("jbgomez", "xxxxxx", "jbgomez2@jbgomez-pc", "Soy el cliente 1", License.lic1);
Client c2 = new Client("jbgomez2", "xxxxxx", "jbgomez@jbgomez-pc", "Soy el cliente 2", License.lic1);
c1.Open();
c2.Open();
//sleep 30 sec
Thread.Sleep(30000);
Console.WriteLine(c1.GetMessage());
Console.WriteLine(c2.GetMessage());
c1.Close();
c2.Close();
Console.ReadLine();
}
}
}
When I run program only it execute one event, I get on the console:
Soy el hilo de SendMessageOnRosterEnd: 9 Soy el cliente 1
It should run four events....
Two SendMessageOnRosterEnd and two xmppClient_OnMessage
Why this events doesn't run?
I'm sorry for my bad english

Regards,
Juan