You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
13 KiB

2 years ago
  1. using Euro.Transfer.Base;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Reflection;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. namespace Euro.Transfer
  12. {
  13. public partial class FormTransfer : Form
  14. {
  15. //用於存放任务项
  16. private Hashtable hashTasks = new Hashtable();
  17. //是否停止運行
  18. private bool bRun = true;
  19. //这里在窗体上没有拖拽一个NotifyIcon控件,而是在这里定义了一个变量
  20. private NotifyIcon notifyIcon = null;
  21. public FormTransfer()
  22. {
  23. InitializeComponent();
  24. Init();
  25. InitialTray();
  26. }
  27. #region 自定义方法
  28. public void RunTasks()
  29. {
  30. try
  31. {
  32. //加载工作项
  33. if (this.hashTasks.Count == 0)
  34. {
  35. //获取configSections节点
  36. var configSections = ServiceTools.GetConfigSections();
  37. foreach (XmlNode section in configSections)
  38. {
  39. //过滤注释节点(如section中还包含其它节点需过滤)
  40. if (section.Name.ToLower() == nameof(section))
  41. {
  42. //创建每个节点的配置对象
  43. var sectionName = section.Attributes["name"].Value.Trim();
  44. var sectionType = section.Attributes["type"].Value.Trim();
  45. //程序集名称
  46. var assemblyName = sectionType.Split(',')[1];
  47. //完整类名
  48. var classFullName = assemblyName + ".Jobs." + sectionName + ".Config";
  49. //创建配置对象
  50. var config = (ServiceConfig)Assembly.Load(assemblyName).CreateInstance(classFullName);
  51. //创建工作对象
  52. var job = (ServiceTask)Assembly.Load(config.Assembly.Split(',')[1]).CreateInstance(config.Assembly.Split(',')[0]);
  53. job.ConfigObject = config;
  54. //将工作对象加载进HashTable
  55. this.hashTasks.Add(sectionName, job);
  56. }
  57. }
  58. }
  59. //执行工作项
  60. if (this.hashTasks.Keys.Count > 0)
  61. {
  62. foreach (ServiceTask task in hashTasks.Values)
  63. {
  64. //插入一个新的请求到线程池
  65. if (ThreadPool.QueueUserWorkItem(ThreadCallBack, task))
  66. {
  67. //方法成功排入队列
  68. }
  69. else
  70. {
  71. //失败
  72. }
  73. }
  74. }
  75. }
  76. catch (Exception error)
  77. {
  78. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, error.ToString(), true);
  79. }
  80. }
  81. private void StopTasks()
  82. {
  83. //停止
  84. if (this.hashTasks != null)
  85. {
  86. this.hashTasks.Clear();
  87. }
  88. }
  89. /// <summary>
  90. /// 线程池回调方法
  91. /// </summary>
  92. /// <param name="state"></param>
  93. private void ThreadCallBack(Object state)
  94. {
  95. while (bRun)
  96. {
  97. ((ServiceTask)state).StartJob();
  98. //休眠1秒
  99. Thread.Sleep(1000);
  100. }
  101. }
  102. #endregion 自定义方法
  103. private void BtnSure_Click(object sender, EventArgs e)
  104. {
  105. try
  106. {
  107. var bError = true;
  108. do
  109. {
  110. var sWeeks = "";
  111. var dicUpdKeys = new Dictionary<string, string>();
  112. if (ckWeek_1.Checked)
  113. {
  114. sWeeks += "1,";
  115. }
  116. if (ckWeek_2.Checked)
  117. {
  118. sWeeks += "2,";
  119. }
  120. if (ckWeek_3.Checked)
  121. {
  122. sWeeks += "3,";
  123. }
  124. if (ckWeek_4.Checked)
  125. {
  126. sWeeks += "4,";
  127. }
  128. if (ckWeek_5.Checked)
  129. {
  130. sWeeks += "5,";
  131. }
  132. if (ckWeek_6.Checked)
  133. {
  134. sWeeks += "6,";
  135. }
  136. if (ckWeek_7.Checked)
  137. {
  138. sWeeks += "0";
  139. }
  140. dicUpdKeys.Add("TransferWeeks", sWeeks);
  141. dicUpdKeys.Add("TransferTime", dateTime.Text);
  142. dicUpdKeys.Add("WriteWordPath", txtPath.Text);
  143. foreach (string key in dicUpdKeys.Keys)
  144. {
  145. var bOk = Common.UpdateAppSettings(key, dicUpdKeys[key]);
  146. if (!bOk)
  147. {
  148. bError = false;
  149. break;
  150. }
  151. }
  152. }
  153. while (false);
  154. if (bError)
  155. {
  156. MessageBox.Show("修改成功");
  157. }
  158. else
  159. {
  160. MessageBox.Show("修改失敗");
  161. }
  162. }
  163. catch (Exception error)
  164. {
  165. MessageBox.Show("修改失敗");
  166. ServiceTools.WriteLog(ServiceBase.Errorlog_Path, error.ToString(), true);
  167. }
  168. }
  169. private void BtnStart_Click(object sender, EventArgs e)
  170. {
  171. btnStart.Enabled = false;
  172. btnEnd.Enabled = true;
  173. bRun = true;
  174. this.RunTasks();
  175. scrollingText1.BackgroundBrush =
  176. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  177. Color.Red, Color.Blue,
  178. LinearGradientMode.Horizontal);
  179. scrollingText1.ForeColor = Color.Yellow;
  180. scrollingText1.ScrollText = "運行中...";
  181. scrollingText1.ScrollDirection = ScrollingTextControl.ScrollDirection.LeftToRight;
  182. scrollingText1.Enabled = true;
  183. }
  184. private void BtnEnd_Click(object sender, EventArgs e)
  185. {
  186. btnStart.Enabled = true;
  187. btnEnd.Enabled = false;
  188. bRun = false;
  189. this.StopTasks();
  190. scrollingText1.BackgroundBrush =
  191. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  192. Color.LightGray, Color.LightGray,
  193. LinearGradientMode.Horizontal);
  194. scrollingText1.ForeColor = Color.LightSlateGray;
  195. scrollingText1.ScrollText = "已停止";
  196. scrollingText1.Enabled = false;
  197. }
  198. private async void Init()
  199. {
  200. var sWriteWordPath = Common.GetAppSetting("WriteWordPath");
  201. var sTransferWeeks = Common.GetAppSetting("TransferWeeks");
  202. var sTransferTime = Common.GetAppSetting("TransferTime");
  203. dateTime.Text = sTransferTime;
  204. txtPath.Text = sWriteWordPath;
  205. if (sTransferWeeks.IndexOf("1") > -1)
  206. {
  207. ckWeek_1.Checked = true;
  208. }
  209. if (sTransferWeeks.IndexOf("2") > -1)
  210. {
  211. ckWeek_2.Checked = true;
  212. }
  213. if (sTransferWeeks.IndexOf("3") > -1)
  214. {
  215. ckWeek_3.Checked = true;
  216. }
  217. if (sTransferWeeks.IndexOf("4") > -1)
  218. {
  219. ckWeek_4.Checked = true;
  220. }
  221. if (sTransferWeeks.IndexOf("5") > -1)
  222. {
  223. ckWeek_5.Checked = true;
  224. }
  225. if (sTransferWeeks.IndexOf("6") > -1)
  226. {
  227. ckWeek_6.Checked = true;
  228. }
  229. if (sTransferWeeks.IndexOf("0") > -1)
  230. {
  231. ckWeek_7.Checked = true;
  232. }
  233. scrollingText1.BackgroundBrush =
  234. new LinearGradientBrush(this.scrollingText1.ClientRectangle,
  235. Color.LightGray, Color.LightGray,
  236. LinearGradientMode.Horizontal);
  237. scrollingText1.ForeColor = Color.LightSlateGray;
  238. scrollingText1.ScrollText = "請點擊“啟動”運行小助手";
  239. scrollingText1.Enabled = false;
  240. var url = Common.GetAppSetting("EURO_MsgServerUrl");
  241. await ServiceTask.hubClient.RunAsync(url);
  242. }
  243. /// <summary>
  244. /// 窗体关闭的单击事件
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  249. {
  250. e.Cancel = true;
  251. //通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”
  252. this.Hide();
  253. }
  254. private void InitialTray()
  255. {
  256. //隐藏主窗体
  257. this.Hide();
  258. //实例化一个NotifyIcon对象
  259. notifyIcon = new NotifyIcon
  260. {
  261. //托盘图标气泡显示的内容
  262. BalloonTipText = "正在後臺運行",
  263. //托盘图标显示的内容
  264. Text = "奕達文字檔小助手"
  265. };
  266. //注意:下面的路径可以是绝对路径、相对路径。但是需要注意的是:文件必须是一个.ico格式
  267. var sIconPath = Application.StartupPath.ToString() + @"\eurtoran_ico.ico";
  268. notifyIcon.Icon = new Icon(sIconPath);
  269. //true表示在托盘区可见,false表示在托盘区不可见
  270. notifyIcon.Visible = true;
  271. //气泡显示的时间(单位是毫秒)
  272. notifyIcon.ShowBalloonTip(2000);
  273. //notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseClick);
  274. notifyIcon.MouseDoubleClick += new MouseEventHandler(notifyIcon_MouseClick);
  275. ////设置二级菜单
  276. //MenuItem setting1 = new MenuItem("二级菜单1");
  277. //MenuItem setting2 = new MenuItem("二级菜单2");
  278. //MenuItem setting = new MenuItem("一级菜单", new MenuItem[]{setting1,setting2});
  279. //帮助选项,这里只是“有名无实”在菜单上只是显示,单击没有效果,可以参照下面的“退出菜单”实现单击事件
  280. //MenuItem help = new MenuItem("帮助");
  281. ////关于选项
  282. //MenuItem about = new MenuItem("关于");
  283. //退出菜单项
  284. var exit = new MenuItem("退出");
  285. exit.Click += new EventHandler(exit_Click);
  286. ////关联托盘控件
  287. //注释的这一行与下一行的区别就是参数不同,setting这个参数是为了实现二级菜单
  288. //MenuItem[] childen = new MenuItem[] { setting, help, about, exit };
  289. var childen = new MenuItem[] { exit };
  290. //MenuItem[] childen = new MenuItem[] { help, about, exit };
  291. notifyIcon.ContextMenu = new ContextMenu(childen);
  292. //窗体关闭时触发
  293. this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
  294. }
  295. /// <summary>
  296. /// 鼠标单击
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
  301. {
  302. //鼠标左键单击
  303. if (e.Button == MouseButtons.Left)
  304. {
  305. //如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见
  306. //if (this.Visible == true)
  307. //{
  308. // this.Visible = false;
  309. //}
  310. //else
  311. //{
  312. // this.Visible = true;
  313. // this.Activate();
  314. //}
  315. this.Visible = true;
  316. this.Activate();
  317. }
  318. }
  319. /// <summary>
  320. /// 退出选项
  321. /// </summary>
  322. /// <param name="sender"></param>
  323. /// <param name="e"></param>
  324. private void exit_Click(object sender, EventArgs e)
  325. {
  326. //退出程序
  327. Environment.Exit(0);
  328. }
  329. }
  330. }