MessageCenterService.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class MessageCenterService
  12. {
  13. public readonly static MessageCenterService Instance = new MessageCenterService();
  14. private MessageCenterService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void dosomething()
  23. {
  24. while (true)
  25. {
  26. string data = RedisDbconn.Instance.RPop<string>("MsgPersonalQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. WebCMSEntities db = new WebCMSEntities();
  32. MsgPersonal msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgPersonal>(data);
  33. db.MsgPersonal.Add(msg);
  34. db.SaveChanges();
  35. db.Dispose();
  36. }
  37. catch (Exception ex)
  38. {
  39. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "消息中心队列异常");
  40. }
  41. }
  42. else
  43. {
  44. Thread.Sleep(60000);
  45. }
  46. }
  47. }
  48. }
  49. }