2015年11月23日 星期一

[參考程式碼][WebService]最佳化呼叫WebService的頻率(Winform版本)

Form的外觀:

Form1.cs:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Threading;
  12.  
  13. namespace TPEPublicValuePriceWinForm
  14. {
  15. public partial class Form1 : Form
  16. {
  17. //臺北市的timer
  18. System.Windows.Forms.Timer tpeProgrssBarTimer = new System.Windows.Forms.Timer();
  19. System.Windows.Forms.Timer tpeQueryTPETimer = new System.Windows.Forms.Timer();
  20. System.Windows.Forms.Timer tpeAvgSpeedTimer = new System.Windows.Forms.Timer();
  21.  
  22. //新北市的timer
  23. System.Windows.Forms.Timer newTpeProgrssBarTimer = new System.Windows.Forms.Timer();
  24. System.Windows.Forms.Timer newTpeQueryTPETimer = new System.Windows.Forms.Timer();
  25. System.Windows.Forms.Timer newTpeAvgSpeedTimer = new System.Windows.Forms.Timer();
  26.  
  27. bool tpeWCFCalling = false;
  28. bool newTpeWCFCalling = false;
  29.  
  30.  
  31. //臺北市的狀態記錄
  32. int tpeNowCount;//現在執行完畢的筆數
  33. int tpeOldCount;//五秒前的執行完畢的筆數
  34. int tpeTotalCount;//臺北市總筆數
  35.  
  36. //新北市的狀態記錄
  37. int newTpeNowCount;//現在執行完畢的筆數
  38. int newTpeOldCount;//五秒前的執行完畢的筆數
  39. int newTpeTotalCount;//新北市總筆數
  40.  
  41. //臺北市的平均執行速度狀態記錄
  42. int tpeOldMinuteCount; //一分鐘前執行完畢的筆數
  43. int tpeNowMinuteCount; //目前執行完畢的筆數
  44.  
  45. //新北市的平均執行速度狀態記錄
  46. int newTpeOldMinuteCount; //一分鐘前執行完畢的筆數
  47. int newTpeNowMinuteCount; //目前執行完畢的筆數
  48.  
  49. string tpeCityCode = "0000"; //臺北市城市代碼
  50. string newTpeCityCode = "0001";//新北市程式代碼
  51.  
  52.  
  53.  
  54. public Form1()
  55. {
  56.  
  57. InitializeComponent();
  58. init();
  59.  
  60. }
  61.  
  62. private void init()
  63. {
  64. //初始化筆數
  65. using (rehouseEntities db = new rehouseEntities())
  66. {
  67. //臺北市總筆數
  68. TPETotalCountLbl.Text = db.pft23.Where(p => p.city_code == tpeCityCode).Count().ToString();
  69. tpeTotalCount = Convert.ToInt32(TPETotalCountLbl.Text);
  70. //臺北市執行完畢筆數
  71. TPEFinCountLbl.Text = GetTpeFinCountNow().ToString();
  72.  
  73. //新北市總筆數
  74. NewTPETotalCountLbl.Text = db.pft23.Where(p => p.city_code == newTpeCityCode).Count().ToString();
  75. newTpeTotalCount = Convert.ToInt32(NewTPETotalCountLbl.Text);
  76. //新北市執行完畢筆數
  77. NewTPEFinCountLbl.Text = GetNewTpeFinCountNow().ToString();
  78.  
  79. }
  80.  
  81. //為timer設定事件
  82. //臺北市
  83. tpeProgrssBarTimer.Interval = Convert.ToInt16(Config.ProgressBarCheckTime);//3 secs顯示一次進度
  84. tpeProgrssBarTimer.Tick += new System.EventHandler(tpeProgrssBarTimer_Tick);
  85. tpeQueryTPETimer.Interval = Convert.ToInt16(Config.SleepTime);//10 secs檢查一次是否有正在查詢
  86. tpeQueryTPETimer.Tick += new System.EventHandler(tpeQueryTPETimer_Tick);
  87. tpeAvgSpeedTimer.Interval = Convert.ToInt32(Config.AverageSpeedCheckTime);//60 secs檢查一次是否有正在查詢
  88. tpeAvgSpeedTimer.Tick += new System.EventHandler(queryTPEAverageSpeed_Tick);
  89. //新北市
  90. newTpeProgrssBarTimer.Interval = Convert.ToInt16(Config.ProgressBarCheckTime);//3 secs顯示一次進度
  91. newTpeProgrssBarTimer.Tick += new System.EventHandler(newTpeProgrssBarTimer_Tick);
  92. newTpeQueryTPETimer.Interval = Convert.ToInt16(Config.SleepTime);//10 secs檢查一次是否有正在查詢
  93. newTpeQueryTPETimer.Tick += new System.EventHandler(newTpeQueryTPETimer_Tick);
  94. newTpeAvgSpeedTimer.Interval = Convert.ToInt32(Config.AverageSpeedCheckTime);//60 secs檢查一次是否有正在查詢
  95. newTpeAvgSpeedTimer.Tick += new System.EventHandler(queryNewTPEAverageSpeed_Tick);
  96. }
  97.  
  98. #region control event
  99.  
  100. //按下查詢臺北
  101. private void QueryTPEBtn_Click(object sender, EventArgs e)
  102. {
  103. TPETimerStart();
  104. exeTPEStatusLbl.Text = "執行中";
  105. exeTPEStatusLbl.Text = "執行中";
  106. tpeNowCount = GetTpeFinCountNow();
  107. tpeOldCount = tpeNowCount;
  108. tpeNowMinuteCount = tpeNowCount;
  109. tpeOldMinuteCount = tpeNowCount;
  110. MessageBox.Show("已開始查詢臺北!");
  111. }
  112.  
  113. //停止查詢臺北
  114. private void stopTPEBtn_Click(object sender, EventArgs e)
  115. {
  116. tpeProgrssBarTimer.Stop();
  117. tpeQueryTPETimer.Stop();
  118. tpeAvgSpeedTimer.Stop();
  119. exeTPEStatusLbl.Text = "停止";
  120. MessageBox.Show("已停止查詢臺北!");
  121. }
  122.  
  123. //按下查詢新北
  124. private void QueryNewTPEBtn_Click(object sender, EventArgs e)
  125. {
  126. NewTPETimerStart();
  127. exeNewTPEStatusLbl.Text = "執行中";
  128.  
  129. exeNewTPEStatusLbl.Text = "執行中";
  130. newTpeNowCount = GetNewTpeFinCountNow();
  131. newTpeOldCount = newTpeNowCount;
  132. newTpeNowMinuteCount = newTpeNowCount;
  133. newTpeOldMinuteCount = newTpeNowCount;
  134. MessageBox.Show("已開始查詢新北!");
  135. }
  136.  
  137. //停止查詢新北
  138. private void stopNewTPEBtn_Click(object sender, EventArgs e)
  139. {
  140. newTpeProgrssBarTimer.Stop();
  141. newTpeQueryTPETimer.Stop();
  142. newTpeAvgSpeedTimer.Stop();
  143. exeNewTPEStatusLbl.Text = "停止";
  144. MessageBox.Show("已停止查詢新北!");
  145. }
  146.  
  147.  
  148. #endregion
  149.  
  150. #region Timer Event
  151. //持續更新畫面上查詢臺北的進度
  152. private void tpeProgrssBarTimer_Tick(object sender, EventArgs e)
  153. {
  154.  
  155. TPEFinCountLbl.Text = GetTpeFinCountNow().ToString();
  156. int temp = DateTime.Now.Millisecond % 3;
  157. if(temp ==0)
  158. {
  159. exeTPEStatusLbl.Text = "執行中。。。。。。";
  160. }
  161. else if (temp == 1)
  162. {
  163. exeTPEStatusLbl.Text = "執行中。。。。";
  164. }
  165. else if (temp == 2)
  166. {
  167. exeTPEStatusLbl.Text = "執行中。。";
  168. }
  169. }
  170.  
  171. //每3秒持續呼叫臺北wcf一次
  172. private void tpeQueryTPETimer_Tick(object sender, EventArgs e)
  173. {
  174. tpeNowCount = GetTpeFinCountNow();//現在執行完畢的筆數
  175. //尚未達到總筆數的時候
  176. if (tpeNowCount != tpeTotalCount)
  177. {
  178. //等上次呼叫的兩百筆執行完才繼續呼叫
  179.  
  180. if (tpeNowCount == tpeOldCount
  181. || tpeNowCount >= tpeOldCount + Convert.ToInt32(Config.OnceTakeCount)
  182. - Convert.ToInt32(Config.OnceTakeCount) * Convert.ToDouble(Config.NoResponseRate))
  183. {
  184. //上次呼叫的執行完的時候,接續著呼叫下一個兩百筆wcf
  185. //或是只差一點就執行完畢兩百筆的時候,接著呼叫下一個兩百筆
  186. //呼叫之前,先記錄目前執行完畢的筆數
  187. //如果正在呼叫中,就不要一直呼叫,不然iis會過載
  188. if (tpeWCFCalling == false)
  189. {
  190. tpeOldCount = GetTpeFinCountNow();
  191. tpeWCFCalling = true;
  192. Task.Run(() => QueryTPEByService());
  193. }
  194. }
  195. else if(tpeNowCount < tpeOldCount + Convert.ToInt32(Config.OnceTakeCount))
  196. {
  197. //還沒執行完上次呼叫的兩百筆,什麼都不做
  198. }
  199. else
  200. {
  201. //
  202. }
  203.  
  204. }
  205. else
  206. {
  207. //已經到達總筆數
  208. TPEFinCountLbl.Text = tpeNowCount.ToString();
  209. exeTPEStatusLbl.Text = "已執行完畢!";
  210. StopTpeTimer();
  211. }
  212.  
  213. }
  214.  
  215. //確認每分鐘臺北查詢速度
  216. private void queryTPEAverageSpeed_Tick(object sender, EventArgs e)
  217. {
  218. using (rehouseEntities db = new rehouseEntities())
  219. {
  220. tpeNowMinuteCount = GetTpeFinCountNow();
  221. string avgSpeedResult = DateTime.Now.ToString("hh:mm") + "完成筆數:" + Convert.ToString(tpeNowMinuteCount - tpeOldMinuteCount);
  222. TPEAvgSpeedTxt.Text = TPEAvgSpeedTxt.Text + avgSpeedResult + "\r\n";
  223. //每三分鐘檢查一次是否速度降為零
  224. //是的話,就重送
  225. if (DateTime.Now.Minute % 3 == 0 && tpeNowCount != tpeTotalCount)
  226. {
  227. if (tpeNowMinuteCount - tpeOldMinuteCount == 0)
  228. {
  229. tpeOldCount = tpeNowCount;
  230. }
  231. }
  232. tpeOldMinuteCount = GetTpeFinCountNow();//記錄一分鐘前執行完畢的筆數
  233. }
  234. }
  235.  
  236.  
  237. //持續更新畫面上查詢新北的進度
  238. private void newTpeProgrssBarTimer_Tick(object sender, EventArgs e)
  239. {
  240.  
  241. NewTPEFinCountLbl.Text = GetNewTpeFinCountNow().ToString();
  242. int temp = DateTime.Now.Millisecond % 3;
  243. if (temp == 0)
  244. {
  245. exeNewTPEStatusLbl.Text = "執行中。。";
  246. }
  247. else if (temp == 1)
  248. {
  249. exeNewTPEStatusLbl.Text = "執行中。。。。。。";
  250. }
  251. else if (temp == 2)
  252. {
  253. exeNewTPEStatusLbl.Text = "執行中。。。。";
  254. }
  255. }
  256.  
  257. //每五秒持續呼叫新北wcf一次
  258. private void newTpeQueryTPETimer_Tick(object sender, EventArgs e)
  259. {
  260.  
  261. newTpeNowCount = GetNewTpeFinCountNow();//現在執行完畢的筆數
  262. //尚未達到總筆數的時候
  263. if (newTpeNowCount != newTpeTotalCount)
  264. {
  265. //等上次呼叫的兩百筆執行完才繼續呼叫
  266. if (newTpeNowCount == newTpeOldCount
  267. || newTpeNowCount >= newTpeOldCount + Convert.ToInt32(Config.OnceTakeCount)
  268. - Convert.ToInt32(Config.OnceTakeCount) * Convert.ToDouble(Config.NoResponseRate))
  269. {
  270. //上次呼叫的執行完的時候,接續著呼叫下一個兩百筆wcf
  271. //或是只差一點就執行完畢兩百筆的時候,接著呼叫下一個兩百筆
  272. //呼叫之前,先記錄目前執行完畢的筆數
  273. //如果正在呼叫中,就不要一直呼叫,不然iis會過載
  274. if (newTpeWCFCalling == false)
  275. {
  276. newTpeOldCount = GetNewTpeFinCountNow();
  277. newTpeWCFCalling = true;
  278. Task.Run(() => QueryNewTPEByService());
  279. }
  280. }
  281. else if (newTpeNowCount < newTpeOldCount + Convert.ToInt32(Config.OnceTakeCount))
  282. {
  283. //還沒執行完上次呼叫的兩百筆,什麼都不做
  284. }
  285. else
  286. {
  287. //
  288.  
  289. }
  290.  
  291. }
  292. else
  293. {
  294. //已經到達總筆數
  295. NewTPEFinCountLbl.Text = newTpeNowCount.ToString();
  296. exeNewTPEStatusLbl.Text = "已執行完畢!";
  297. StopNewTpeTimer();
  298. }
  299.  
  300.  
  301. }
  302.  
  303. //確認每分鐘新北查詢速度
  304. private void queryNewTPEAverageSpeed_Tick(object sender, EventArgs e)
  305. {
  306. using (rehouseEntities db = new rehouseEntities())
  307. {
  308. newTpeNowMinuteCount = GetNewTpeFinCountNow();
  309. string avgSpeedResult = DateTime.Now.ToString("hh:mm") + "完成筆數:" + Convert.ToString(newTpeNowMinuteCount - newTpeOldMinuteCount);
  310. NewTPEAvgSpeedTxt.Text = NewTPEAvgSpeedTxt.Text + avgSpeedResult + "\r\n";
  311. //每三分鐘檢查一次是否速度降為零
  312. //是的話,就重送
  313. if (DateTime.Now.Minute % 3 == 0 && newTpeNowCount != newTpeTotalCount)
  314. {
  315. //如果新北市官網的回應太少導致ResponseRate太低導致該分鐘的速度為0的話,就再度重送
  316. if (newTpeNowMinuteCount - newTpeOldMinuteCount == 0)
  317. {
  318. newTpeOldCount = newTpeNowCount;
  319. }
  320. }
  321.  
  322. newTpeOldMinuteCount = GetNewTpeFinCountNow();//記錄一分鐘前執行完畢的筆數
  323. }
  324. }
  325.  
  326. private void StopTpeTimer()
  327. {
  328. tpeProgrssBarTimer.Stop();
  329. tpeQueryTPETimer.Stop();
  330. tpeAvgSpeedTimer.Stop();
  331. }
  332.  
  333. private void StopNewTpeTimer()
  334. {
  335. newTpeProgrssBarTimer.Stop();
  336. newTpeQueryTPETimer.Stop();
  337. newTpeAvgSpeedTimer.Stop();
  338. }
  339.  
  340.  
  341. #endregion
  342.  
  343. #region custom method
  344.  
  345. //取得臺北目前查詢完畢的資料筆數
  346. private int GetTpeFinCountNow()
  347. {
  348. using (rehouseEntities db = new rehouseEntities())
  349. {
  350. return db.pft23.Where(p => (p.devo_value != null || p.ground_price != null) && p.city_code == tpeCityCode).Count();
  351. }
  352. }
  353.  
  354. //呼叫wcf查詢臺北
  355. private void QueryTPEByService()
  356. {
  357.  
  358. TPELandPublicValuePriceService.ServiceClient sc = new TPELandPublicValuePriceService.ServiceClient();
  359. using (rehouseEntities db = new rehouseEntities())
  360. {
  361. var pft23s = db.pft23.Where(p => p.devo_value == null && p.ground_price == null && p.city_code == tpeCityCode).Take(Convert.ToInt16(Config.OnceTakeCount));
  362. foreach (var p23 in pft23s)
  363. {
  364. sc.GetTPELandPublicValuePrice(p23.zip_code, p23.session_id, p23.area_no);
  365. }
  366.  
  367. }
  368. sc.Close();
  369. tpeWCFCalling = false;//呼叫完畢
  370. }
  371.  
  372. //啟動臺北timer
  373. private void TPETimerStart()
  374. {
  375. tpeProgrssBarTimer.Start();
  376. tpeQueryTPETimer.Start();
  377. tpeAvgSpeedTimer.Start();
  378. }
  379.  
  380. //取得新北目前查詢完畢的資料筆數
  381. private int GetNewTpeFinCountNow()
  382. {
  383. using (rehouseEntities db = new rehouseEntities())
  384. {
  385. return db.pft23.Where(p => (p.devo_value != null || p.ground_price != null) && p.city_code == newTpeCityCode).Count();
  386. }
  387. }
  388.  
  389. //呼叫wcf查詢臺北
  390. private void QueryNewTPEByService()
  391. {
  392. TPELandPublicValuePriceService.ServiceClient sc = new TPELandPublicValuePriceService.ServiceClient();
  393. using (rehouseEntities db = new rehouseEntities())
  394. {
  395. var pft23s = db.pft23.Where(p => p.devo_value == null && p.ground_price == null && p.city_code == newTpeCityCode).Take(Convert.ToInt16(Config.OnceTakeCount));
  396. foreach (var p23 in pft23s)
  397. {
  398. sc.GetNewTPELandPublicValuePrice(p23.zip_code, p23.session_id, p23.area_no);
  399. }
  400.  
  401. }
  402. sc.Close();
  403. newTpeWCFCalling = false;//呼叫完畢
  404. }
  405.  
  406. //啟動新北timer
  407. private void NewTPETimerStart()
  408. {
  409. newTpeProgrssBarTimer.Start();
  410. newTpeQueryTPETimer.Start();
  411. newTpeAvgSpeedTimer.Start();
  412. }
  413. #endregion
  414.  
  415.  
  416. }
  417. }

Config.cs:方便快速存取appsetting用的類別
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Configuration;
  7.  
  8. namespace TPEPublicValuePriceWinForm
  9. {
  10. public class Config
  11. {
  12. public static string SleepTime = GetAppSettings("SleepTime");
  13. public static string ProgressBarCheckTime = GetAppSettings("ProgressBarCheckTime");
  14. public static string AverageSpeedCheckTime = GetAppSettings("AverageSpeedCheckTime");
  15. public static string ResponseLostTime = GetAppSettings("ResponseLostTime");
  16. public static string AutoStopCount = GetAppSettings("AutoStopCount");
  17. public static string AutoInitCount = GetAppSettings("AutoInitCount");
  18. public static string OnceTakeCount = GetAppSettings("OnceTakeCount");
  19. public static string NoResponseRate = GetAppSettings("NoResponseRate");
  20.  
  21. private static string GetAppSettings(string key)
  22. {
  23. return ConfigurationManager.AppSettings[key];
  24. }
  25. }
  26. }
  27.  

app.config檔裡面參考的參數:主要是設定timer每多少秒數要執行一次
<appSettings>  
<add key="SleepTime" value="3000"/>
<add key="ProgressBarCheckTime" value="3000"/>
<add key="AverageSpeedCheckTime" value="60000"/>
<add key="ResponseLostTime" value="150000"/>
<add key="AutoStopCount" value="10"/>
<add key="AutoInitCount" value="30"/>
<add key="OnceTakeCount" value="200"/>
<add key="NoResponseRate" value="0.1"/>

</appSettings>