Skip to content

Instantly share code, notes, and snippets.

@zhuqling
Created March 11, 2013 03:49
Show Gist options
  • Save zhuqling/5131766 to your computer and use it in GitHub Desktop.
Save zhuqling/5131766 to your computer and use it in GitHub Desktop.
使用EventingBasicConsumer监听RabbitMQ消息, 不要求应答信息, 更新UI
public delegate void UpdateMsgBodyDelegate(string aString);
// 使用Consumer监控匿名Queue
var evtConsumer = new EventingBasicConsumer(){ Model = _chan};
evtConsumer.Received += Notify;
_chan.BasicConsume(
queue: qName,
noAck: true, // 不要返回ACK
consumer: evtConsumer
);
private void Notify(IBasicConsumer sender, BasicDeliverEventArgs args)
{
IBasicProperties msgProps = args.BasicProperties;
string msgBody = Encoding.UTF8.GetString(args.Body); // 使用UTF8编码,返回中文错误消息
// 此函数是由其它线程调用,更新UI主线程需使用delegate
lblResult.BeginInvoke(new UpdateMsgBodyDelegate(UpdateMsgBody), msgBody);
}
private void UpdateMsgBody(string body)
{
lblResult.Text = string.Format("计算结果:{0}", body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment