Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. public void RefreshConnect()
  2. {
  3. new System.Timers.Timer(1000) { Enabled = true }.Elapsed += delegate
  4. {
  5. var connected = CheckNet.CheckURL(StrSite.TestConnect);
  6. while(connected)
  7. if (connected)
  8. {
  9. CheckProcessClose.Enabled = true;
  10. OffSite4Game.Enabled = true;
  11. TracePing.Enabled = true;
  12. Checker4Game.Enabled = true;
  13. OffSiteForum.Enabled = true;
  14. SupportGame.Enabled = true;
  15. MRTDownload.Enabled = true;
  16. }
  17. else
  18. {
  19. CheckProcessClose.Enabled = false;
  20. OffSite4Game.Enabled = false;
  21. TracePing.Enabled = false;
  22. Checker4Game.Enabled = false;
  23. OffSiteForum.Enabled = false;
  24. SupportGame.Enabled = false;
  25. MRTDownload.Enabled = false;
  26. Refresh();
  27. }
  28. Invoke(new MethodInvoker(() => { BadConnect.Visible = !connected; }));
  29. };
  30. }
  31.  
  32. Task.Factory.StartNew(() => RefreshConnect());
  33.  
  34. class ConnectionMonitor : IDisposable
  35. {
  36. private bool urlAvailable = false;
  37. private bool isActive = false;
  38. private System.Threading.Timer timer;
  39.  
  40. public string Url { get; private set; }
  41.  
  42. public event EventHandler ConnectionStateChanged;
  43.  
  44. public ConnectionMonitor(string checkingUrl) { Url = checkingUrl; }
  45.  
  46. public void Start()
  47. {
  48. isActive = true;
  49. timer = new Timer(CheckUrl, null, 1000, Timeout.Infinite);
  50. }
  51.  
  52. public void Stop()
  53. {
  54. isActive = false;
  55. }
  56.  
  57. private void CheckUrl(object state)//сигнатура для TimerCalback
  58. {
  59. var checkResult = CheckNet.CheckURL(StrSite.TestConnect);
  60. if(checkResult ^ urlAvailable)
  61. {
  62. ConnectionStateChanged.Invoke(this, new EventArgs());
  63. urlAvailable = checkResult;
  64. }
  65. timer.Dispose();
  66. if(isActive)
  67. timer = new Timer(CheckUrl, null, 1000, Timeout.Infinite);
  68. }
  69.  
  70. public void Dispose() { timer?.Dispose(); }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement