Advertisement
Guest User

Polling Example

a guest
Jun 14th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.SessionState;
  7. using System.Threading;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. using FM.WebSync.Core;
  11. using System.IO;
  12. using System.Configuration;
  13.  
  14. namespace DBPolling
  15. {
  16. public class Global : System.Web.HttpApplication
  17. {
  18. private bool Running;
  19.  
  20. static string res;
  21.  
  22. void Application_Start(object sender, EventArgs e)
  23. {
  24. // Code that runs on application startup
  25.  
  26.  
  27. Running = true;
  28. Thread t = new Thread(new ThreadStart(PublishLoop));
  29. t.IsBackground = true;
  30. t.Start();
  31. }
  32.  
  33. void Application_End(object sender, EventArgs e)
  34. {
  35. // Code that runs on application shutdown
  36. Running = false;
  37. }
  38.  
  39. void Application_Error(object sender, EventArgs e)
  40. {
  41. // Code that runs when an unhandled error occurs
  42.  
  43. }
  44.  
  45. void Session_Start(object sender, EventArgs e)
  46. {
  47. // Code that runs when a new session is started
  48.  
  49. }
  50.  
  51. void Session_End(object sender, EventArgs e)
  52. {
  53. // Code that runs when a session ends.
  54. // Note: The Session_End event is raised only when the sessionstate mode
  55. // is set to InProc in the Web.config file. If session mode is set to StateServer
  56. // or SQLServer, the event is not raised.
  57.  
  58. }
  59. private void PublishLoop()
  60. {
  61. String prev=String.Copy("");
  62. String next=String.Copy("");
  63. String ConnectionString = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
  64. SqlConnection connection = new SqlConnection(ConnectionString);
  65. SqlCommand command = connection.CreateCommand();
  66. command.CommandText = "select ID from Tab1";
  67. command.Notification = null;
  68.  
  69. while (Running)
  70. {
  71. connection.Open();
  72. using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
  73. {
  74. StreamWriter sw1 = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\next.txt");
  75. while ((reader.Read()))
  76. {
  77. //Response.Write(reader[0].ToString());
  78. next = String.Concat(next,reader[0].ToString());
  79. sw1.WriteLine(next);
  80.  
  81. }
  82. sw1.Close();
  83.  
  84.  
  85. if (!prev.Equals(next))
  86. {
  87. Publisher publisher = new Publisher(new PublisherArgs
  88. {
  89. DomainKey = "c80cb405-eb77-4574-9405-5ba51832f5e6",
  90. DomainName="localhost"
  91. });
  92. Publication publication = publisher.Publish("/test", JSON.Serialize(next));
  93. if (publication.Successful == true)
  94. {
  95. StreamWriter sw = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\error123.txt");
  96. sw.WriteLine("success");
  97. sw.WriteLine(next);
  98. sw.Close();
  99. }
  100. else
  101. {
  102. StreamWriter sw = new StreamWriter("C:\\Users\\Thothathri\\Desktop\\error123.txt");
  103. sw.Write("failed");
  104. sw.Close();
  105. }
  106. prev = String.Copy(next);
  107. next = String.Copy("");
  108. }
  109.  
  110.  
  111. }
  112. Thread.Sleep(5000);
  113.  
  114. }
  115. }
  116.  
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement