Guest User

Untitled

a guest
Jun 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. private void bgwProcessLogin_DoWork(object sender, DoWorkEventArgs e)
  2. {
  3. /*
  4. * Perform at test to see if the background worker has been
  5. * cancelled by the user before attemping to continue to login.
  6. *
  7. * Cancel background worker on any failed attemp to login
  8. */
  9.  
  10. // Start with cancel being false as to reset this if cancel has been set to true
  11. // in the cancel button.
  12. e.Cancel = false;
  13.  
  14. NetworkingTest connection_test = new NetworkingTest();
  15. if (!this.bgwProcessLogin.CancellationPending)
  16. {
  17. // Check local LAN or Wireless connection
  18. if (!connection_test.IsNetworkConnected())
  19. {
  20. // Update label
  21. if (this.lblRegistering.InvokeRequired)
  22. {
  23. this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(UpdateRegisterLabel), "No network connection");
  24. }
  25. else
  26. {
  27. this.lblRegistering.Text = "No network connection";
  28. }
  29. // Failed attemp
  30. this.bgwProcessLogin.CancelAsync();
  31. e.Cancel = true;
  32. return;
  33. }
  34. // Report current progress
  35. this.bgwProcessLogin.ReportProgress(0, "Network connected");
  36. }
  37. else
  38. {
  39. // User cancelled
  40. e.Cancel = true;
  41. return;
  42. }
  43.  
  44. // Test if access to Server is available
  45. if (!this.bgwProcessLogin.CancellationPending)
  46. {
  47. if (!connection_test.IsSIPServerAvailable())
  48. {
  49. // Update label
  50. if (this.lblRegistering.InvokeRequired)
  51. {
  52. this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(UpdateRegisterLabel), "Server unavailable");
  53. }
  54. else
  55. {
  56. this.lblRegistering.Text = "Server unavailable";
  57. }
  58. // Failed attemp
  59. this.bgwProcessLogin.CancelAsync();
  60. e.Cancel = true;
  61. return;
  62. }
  63. // Report current progress
  64. this.bgwProcessLogin.ReportProgress(1, "Server available");
  65. }
  66. else
  67. {
  68. // User cancelled
  69. e.Cancel = true;
  70. return;
  71. }
  72. .
  73. .
  74. .
  75. }
  76.  
  77.  
  78. private void bgwProcessLogin_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  79. {
  80. // Check for any errors
  81. if (e.Error == null)
  82. {
  83. if (e.Cancelled)
  84. {
  85. // User cancelled login or login failed
  86. }
  87. else
  88. {
  89. // Login completed successfully
  90. }
  91. }
  92. else
  93. {
  94. // Something failed display error
  95. this.statusDisplay1.CallStatus = e.Error.Message;
  96. }
  97. }
  98.  
  99.  
  100. private void bgwProcessLogin_ProgressChanged(object sender, ProgressChangedEventArgs e)
  101. {
  102. this.lblRegistering.Text = e.UserState.ToString();
  103. }
  104.  
  105. private void btnCancel_Click(object sender, EventArgs e)
  106. {
  107. // Cancel the logging in process
  108. this.bgwProcessLogin.CancelAsync();
  109. this.lblRegistering.Text = "Logged out";
  110. }
  111.  
  112. public class AbortableBackgroundWorker : BackgroundWorker
  113. {
  114. private Thread workerThread;
  115.  
  116. protected override void OnDoWork(DoWorkEventArgs e)
  117. {
  118. workerThread = Thread.CurrentThread;
  119. try
  120. {
  121. base.OnDoWork(e);
  122. }
  123. catch (ThreadAbortException)
  124. {
  125. e.Cancel = true; //We must set Cancel property to true!
  126. Thread.ResetAbort(); //Prevents ThreadAbortException propagation
  127. }
  128. }
  129.  
  130.  
  131. public void Abort()
  132. {
  133. if (workerThread != null)
  134. {
  135. workerThread.Abort();
  136. workerThread = null;
  137. }
  138. }
  139. }
Add Comment
Please, Sign In to add comment