Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COJust for curiosity, would there be a problem with putting the: wc.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); inside the destructor, rather than the Dispose? (As these are not unmanaged objects)? Is the issue that the dispose may make the object invalid, so you need to do them here instead?
      singulars
    2. CO`IDisposable` is a pattern for implementing deterministic disposal of resources. All resources should be cleaned up in the Dispose method--once it runs, the finalizer is suppressed. The only time the finalizer should run is if the object wasn't disposed properly. The main reason the event handlers are removed is so that we can be certain that `wc_DownloadStringCompleted` isn't received after the object is disposed; that would be unexpected.
      singulars
    3. COZenox, you should never implement a finalizer unless you have to - it's for cleaning up unmanaged memory ONLY. You definitely don't want to refer to other objects in a finalizer as there is no guarantee they haven't already been disposed/finalized. See my answer below & for a longer answer get hold of the framework design guidelines. Basically you should always question whether a finalizer is required and check what it does very carefully. An unhandled exception from a finalizer crashes your application immediately with no chance to recover (how could it recover, it runs in it's own thread).
      singulars
 

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