FromServices.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using WCS.Workflow;
  12. namespace WCS.WFormServices
  13. {
  14. public partial class FromServices : Form
  15. {
  16. private delegate void UIRefresh(string message);//代理
  17. private event UIRefresh fc;//界面更新事件
  18. public FromServices()
  19. {
  20. InitializeComponent();
  21. this.fc += new UIRefresh(ThreadFunction);
  22. }
  23. private void ThreadFunction(string message)
  24. {
  25. this.lblMessage.Text = message;
  26. }
  27. private void btnStart_Click(object sender, EventArgs e)
  28. {
  29. Thread thread = new Thread(StartService);
  30. thread.Start();
  31. }
  32. private void StartService()
  33. {
  34. this.btnStart.Invoke(new MethodInvoker(delegate
  35. {
  36. this.btnStart.Enabled = false;
  37. this.btnClose.Enabled = false;
  38. }));
  39. try
  40. {
  41. this.Invoke(fc, new object[] { "WCS服务正在启动..." });//通过代理调用刷新方法
  42. InitWorkflow.InitData();
  43. QuartzStartup.Start().Wait();
  44. this.Invoke(fc, new object[] { "WCS服务正常启动,正在执行定时任务..." });//通过代理调用刷新方法
  45. }
  46. catch (Exception ex)
  47. {
  48. this.Invoke(fc, new object[] { ex.Message });//通过代理调用刷新方法
  49. this.btnStart.Enabled = true;
  50. }
  51. this.btnStart.Invoke(new MethodInvoker(delegate
  52. {
  53. this.btnClose.Enabled = true;
  54. }));
  55. }
  56. private void btnClose_Click(object sender, EventArgs e)
  57. {
  58. Thread thread = new Thread(StopService);
  59. thread.Start();
  60. }
  61. private void StopService()
  62. {
  63. this.btnStart.Invoke(new MethodInvoker(delegate
  64. {
  65. this.btnClose.Enabled = false;
  66. this.btnStart.Enabled = false;
  67. }));
  68. try
  69. {
  70. this.Invoke(fc, new object[] { "服务正在停止..." });//通过代理调用刷新方法
  71. QuartzStartup.Stop();
  72. this.Invoke(fc, new object[] { "服务已经停止。" });
  73. }
  74. catch (Exception ex)
  75. {
  76. this.Invoke(fc, new object[] { ex.Message });
  77. this.btnClose.Enabled = true;
  78. }
  79. this.btnStart.Invoke(new MethodInvoker(delegate
  80. {
  81. this.btnStart.Enabled = true;
  82. }));
  83. }
  84. private void FromServices_FormClosing(object sender, FormClosingEventArgs e)
  85. {
  86. try
  87. {
  88. QuartzStartup.Stop();
  89. }
  90. catch (Exception ex)
  91. {
  92. MessageBox.Show(ex.Message);
  93. }
  94. }
  95. }
  96. }