Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That is mostly correct except <code>wc</code> is being disposed twice and <code>GetBalance</code> will always be using <code>wc</code> after it's disposed!</p> <p>Edit: a version with that correction:</p> <pre><code>public class Balance : IDisposable { //Constructor WebClient wc; public Balance() { wc = new WebClient(); //Create event handler for the progress changed and download completed events try { wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); } catch { wc.Dispose(); throw; } } ~Balance() { this.Dispose(false); } //Get the current balance for the user that is logged in. //If the balance returned from the server is NULL display error to the user. //Null could occur if the DB has been stopped or the server is down. public void GetBalance(string sipUsername) { //Remove the underscore ( _ ) from the username, as this is not needed to get the balance. sipUsername = sipUsername.Remove(0, 1); string strURL = string.Format("https://www.xxxxxxx.com", sipUsername); //Download only when the webclient is not busy. if (!wc.IsBusy) { // Download the current balance. wc.DownloadStringAsync(new Uri(strURL)); } else { Console.Write("Busy please try again"); } } //return and display the balance after the download has fully completed void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { //Pass the result to the event handler } private bool isDisposed = false; //Dispose of the balance object public void Dispose() { if (!isDisposed) Dispose(true); GC.SuppressFinalize(this); } //Remove the event handlers private void Dispose(bool disposing) { isDisposed = true; if (disposing) { wc.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); wc.Dispose(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload