frmFtpThread.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace LYFZ.Software.MainBusiness.MultipleFileImport
  7. {
  8. public class frmFtpThread
  9. {
  10. static frmFtpThread single = null;
  11. System.Threading.Thread thread;
  12. /// <summary>
  13. /// 开始上传事件
  14. /// </summary>
  15. public delegate void StartUpDataHandler();
  16. public event StartUpDataHandler StartUpDataEvent;
  17. /// <summary>
  18. /// 结束上传时间
  19. /// </summary>
  20. public delegate void EndtartUpDataHandler();
  21. public event EndtartUpDataHandler EndtartUpDataEvent;
  22. List<FTPDataModel> FTPFileList = new List<FTPDataModel>();
  23. public frmFtpThread()
  24. {
  25. }
  26. public static frmFtpThread GetSingleton()
  27. {
  28. if(single==null)
  29. {
  30. single = new frmFtpThread();
  31. }
  32. return single;
  33. }
  34. public void SendDataToService(string jsonData)
  35. {
  36. FTPDataModel mod = new FTPDataModel()
  37. {
  38. b = false,
  39. strJson = jsonData
  40. };
  41. AddFTPFileDataToList(mod);
  42. }
  43. void AddFTPFileDataToList(FTPDataModel mod)
  44. {
  45. FTPFileList.Add(mod);
  46. StartThread();
  47. }
  48. public void StartThread()
  49. {
  50. try
  51. {
  52. if (thread == null || thread.ThreadState == ThreadState.Aborted)
  53. {
  54. ///线程启动处理;
  55. thread = new Thread(new ThreadStart(delegate()
  56. {
  57. for (int i = 0; i < FTPFileList.Count; i++)
  58. {
  59. if (!FTPFileList[i].b)
  60. {
  61. if (StartUpDataEvent != null)
  62. {
  63. StartUpDataEvent();
  64. }
  65. FTPFileList[i].b = true;
  66. HPSocketCS.Extended.RequestData requestData = new HPSocketCS.Extended.RequestData((int)LYFZ.Network.TCPNetworkServer.SerializerRequestCommand.FTPCommand);
  67. requestData.AttachedMessage = FTPFileList[i].strJson;
  68. HPSocketCS.Extended.ReturnData returnData = HPSocketCS.Extended.SerializerDataProcessed.HP_SendSerializerCommandToServer(requestData);
  69. FileUtil.WriteFile(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt",returnData.ReturnStatus+"--"+ FTPFileList[i].strJson);
  70. if (EndtartUpDataEvent != null)
  71. {
  72. EndtartUpDataEvent();
  73. }
  74. }
  75. }
  76. }));
  77. thread.Start();
  78. }
  79. else if (thread.ThreadState == ThreadState.Stopped )
  80. {
  81. thread = null;
  82. StartThread();
  83. }
  84. }
  85. catch(Exception e) {
  86. MessageBoxCustom.Show("出现异常,请重新提交Ftp传片数据!!");
  87. FileUtil.WriteFile(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt", e.Message);
  88. if (EndtartUpDataEvent != null)
  89. {
  90. EndtartUpDataEvent();
  91. }
  92. }
  93. }
  94. }
  95. class FTPDataModel {
  96. public string strJson { get; set; }
  97. public bool b { get; set; }
  98. }
  99. }