I'm programming in C# with XMPP chat protocol and want to wait in the GUI for the response of the counter part. It takes less then 1 seconds, till the counter part response (it's a script), so it's not a problem, if the GUI hangs for that time. I tried with ManualResetEvent, but I just get a timeout exception. I made following code (in extracts):
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;
using System.Threading;
public partial class Form1 : Form
{
static string output_msg;
private ManualResetEvent manualResetEvent;
public Form1()
{
manualResetEvent = new ManualResetEvent(false);
Jid jidSender = new Jid(username + "@" + server);
xmpp = new XmppClientConnection(jidSender.Server);
xmpp.UseStartTLS = true;
xmpp.Open(jidSender.User, password, resource);
xmpp.OnLogin += new ObjectHandler(OnLogin);
// create message receive callback
xmpp.MessageGrabber.Add(new Jid(JID_Receiver), new BareJidComparer(), new MessageCB(MessageCallBack), null);
// send a message
xmpp.Send(new agsXMPP.protocol.client.Message(new Jid(sendTo), MessageType.chat, message));
// wait with ManualResetEvent, till response is available in output_msg
if (!manualResetEvent.WaitOne(3000))
{
throw new TimeoutException();
}
manualResetEvent.Reset();
// show response in a dataGridView . . .
output.AppendText(output_msg); // output is a TextBox, just for debugging
}
private void MessageCallBack(object sender, agsXMPP.protocol.client.Message msg, object data)
{
output_msg = msg.Body;
manualResetEvent.Set();
}
}
How can I wait for response? Is there a alternative to ManualResetEvent?