Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using Core.Mes.ClientFrameWork;
  6. using System.IO;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.Data;
  10. namespace DataTest
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. //Thread thread1 = new Thread(new ThreadStart(GetTestData));
  17. //thread1.Start();
  18. //Console.WriteLine("线程1开始运行...");
  19. //Thread thread2 = new Thread(new ThreadStart(GetTestData));
  20. //thread2.Start();
  21. //Console.WriteLine("线程2开始运行...");
  22. //Console.Read();
  23. string url = ConfigurationManager.AppSettings["RemoteServerUrl"];
  24. ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url);
  25. arevent = new AutoResetEvent(false);
  26. timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_ElapsedEvent);
  27. timer.Interval = 10 * 1000;
  28. timer.Start();
  29. Console.Read();
  30. //#region 多线程
  31. //for (int i = 0; i < 7; i++)
  32. //{
  33. // int a = i;
  34. // count = i;
  35. // ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { GetTestData(a); }));
  36. //}
  37. //Console.Read();
  38. //#endregion
  39. }
  40. private static int j = 0;
  41. private static void Timer_ElapsedEvent(Object obj, System.Timers.ElapsedEventArgs args)
  42. {
  43. #region 多线程
  44. for (int i = 0; i < 5; i++)
  45. {
  46. int a = i;
  47. count = i;
  48. ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { GetTestData(a); }));
  49. }
  50. //j++;
  51. //if (j == 1)
  52. // timer.Stop();
  53. Console.Read();
  54. #endregion
  55. }
  56. private static int count = 0;
  57. private static int errnum = 0;
  58. static AutoResetEvent arevent = null;
  59. static System.Timers.Timer timer=new System.Timers.Timer();
  60. private static void GetTestData(Object a)
  61. {
  62. if (count == 4)
  63. arevent.Set();
  64. else
  65. {
  66. arevent.WaitOne();
  67. }
  68. try
  69. {
  70. Stopwatch watch = new Stopwatch();
  71. watch.Start();
  72. string strOut = "";
  73. DataSet obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "SelectTestUser", null, out strOut) as DataSet;
  74. if (strOut != "")
  75. {
  76. Console.WriteLine("失败:" + strOut);
  77. errnum++;
  78. Console.WriteLine("连接失败次数:{0}", errnum);
  79. }
  80. Console.WriteLine("");
  81. Console.WriteLine("线程{0}开始运行...{1}", a, DateTime.Now.ToString());
  82. watch.Stop();
  83. if (watch.Elapsed.Milliseconds == 30000)
  84. {
  85. Console.WriteLine("执行超时。。。");
  86. return;
  87. }
  88. Console.WriteLine("线程{0}运行时间:{1}ms", a, watch.ElapsedMilliseconds.ToString());
  89. if (obj != null && obj.Tables.Count > 0)
  90. {
  91. //Console.WriteLine("数据:{0}\r\n",obj.Tables[0].Rows[0][0].ToString());
  92. Console.WriteLine("数目:" + obj.Tables[0].Rows.Count.ToString() + "\r\n");
  93. obj.Clear();
  94. obj.Dispose();
  95. GC.Collect();
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine("出错啦,跑不动啦~:"+ex.Message);
  101. }
  102. }
  103. }
  104. }