Guest User

Untitled

a guest
Nov 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. protected override void OnStart(string[] args)
  2. {
  3. while(true){
  4.  
  5. string Query="";
  6.  
  7. Query = "SELECT * FROM 'reportsetting` order by SendingTime;";
  8.  
  9. MySqlConnection con = new MySqlConnection(conn);
  10. MySqlCommand comm = new MySqlCommand(Query, con);
  11. con.Open();
  12. MySqlDataReader dr = comm.ExecuteReader();
  13. while (dr.Read())
  14. {
  15.  
  16. time = dr["SendingTime"].ToString();
  17.  
  18. if ((str = DateTime.Now.ToString("HH:mm")).Equals(time))
  19. {
  20.  
  21. //Execute Function and send reports based on the data from the database.
  22.  
  23. Thread thread = new Thread(sendReports);
  24. thread.Start();
  25.  
  26. }
  27.  
  28.  
  29. }
  30.  
  31.  
  32. //Halt for this Moment
  33.  
  34. while ((str = DateTime.Now.ToString("HH:mm")).Equals(time))
  35. {
  36.  
  37.  
  38. }
  39.  
  40.  
  41. }
  42.  
  43. }
  44.  
  45. public void sendReports() {
  46.  
  47.  
  48.  
  49. }
  50.  
  51. private const string Query = "SELECT * FROM 'reportsetting` order by SendingTime;"
  52.  
  53. protected override void OnStart(string[] args)
  54. {
  55. _timer = new Timer(40 * 1000); // every 40 seconds
  56. _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  57. _timer.Start(); // <- important
  58. }
  59.  
  60. private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  61. {
  62. MySqlConnection con = new MySqlConnection(conn);
  63. MySqlCommand comm = new MySqlCommand(Query, con);
  64. con.Open();
  65. MySqlDataReader dr = comm.ExecuteReader();
  66. while (dr.Read())
  67. {
  68.  
  69. time = dr["SendingTime"].ToString();
  70.  
  71. if ((str = DateTime.Now.ToString("HH:mm")).Equals(time))
  72. {
  73.  
  74. //Execute Function and send reports based on the data from the database.
  75.  
  76. Thread thread = new Thread(sendReports);
  77. thread.Start();
  78. }
  79. }
  80. }
  81.  
  82. private static Timer timer;
  83. private const int TimerInterval = 30000;
  84.  
  85. protected override void OnStart(string[] args)
  86. {
  87. var callback = new TimerCallback(checkDatabase);
  88. this.timer = new Timer(callback, null, 0, TimerInterval);
  89. }
  90.  
  91. private void checkDatabase()
  92. {
  93. string query = "SELECT * FROM 'reportsetting` order by SendingTime;";
  94. using (var con = new MySqlConnection(conn))
  95. using (var cmd = new MySqlCommand(query, con))
  96. {
  97. con.Open();
  98. using (var dr = cmd.ExecuteReader())
  99. {
  100. while (dr.Read())
  101. {
  102. // do some work
  103. }
  104. }
  105. }
  106. }
  107.  
  108. private const int DefaultInterval = 40000;
  109. private int interval = DefaultInterval;
  110. ...
  111.  
  112. while (dr.Read())
  113. {
  114. if (someCondition)
  115. {
  116. // do some work
  117. timer.Change(DefaultInterval, DefaultInterval); // revert to 40 seconds
  118. }
  119. else
  120. {
  121. // no change? increase polling by 10 seconds
  122. interval += 10000;
  123. timer.Change(interval, interval);
  124. }
  125. }
  126.  
  127. bool _IsStop = false;
  128.  
  129. protected override void OnStart(string[] args)
  130. {
  131. base.OnStart(args);
  132.  
  133. while (!this._IsStop)
  134. {
  135.  
  136. this.Process();
  137.  
  138. //40 seconds time out
  139. Thread.Sleep(1000*40); //1000 is 1 second
  140. }
  141.  
  142. //If we reach here
  143. //Do some clean up here
  144.  
  145. }
  146.  
  147.  
  148. protected override void OnStop()
  149. {
  150. //Service is stop by the user on service or by OS
  151. this._IsStop = true;
  152.  
  153. }
  154.  
  155.  
  156. private void Process()
  157. {
  158. string Query = "";
  159.  
  160. // -->>> if you use something like this on your query and little bit setup on your db
  161. // this will give you a more efficient memory usage
  162. Query = "SELECT * FROM reportsetting where [your tag if already sent] = false and SendingTime <= [current date and time] order by SendingTime;";
  163.  
  164. MySqlConnection con = new MySqlConnection(conn);
  165. MySqlCommand comm = new MySqlCommand(Query, con);
  166. con.Open();
  167. MySqlDataReader dr = comm.ExecuteReader();
  168. while (dr.Read())
  169. {
  170. //Execute Function and send reports based on the data from the database.
  171.  
  172. Thread thread = new Thread(sendReports);
  173. thread.Start();
  174.  
  175. //sleep for half a second before proceeding to the next record if any
  176. Thread.Sleep(500);
  177. }
  178. }
  179.  
  180. var threadStart = new ThreadStart(<your method delegate>);
  181. var thread = new Thread(threadStart);
  182. thread.Start();
Add Comment
Please, Sign In to add comment