1

I have a method which returns DataSet from postgressql database.

    ds = stats.loadStats();

I need to update DataSet randomly, once in 3 minutes (for example).

                Random r = new Random();
                var rendom_number = r.Next(0, 179);//3 minutes = 3*60 = 180 sec.
                if (rendom_number == 1)
                {
                   //reload Dataset here
                }

once event is triggered - I need to dispose of the old Dataset and update it with the new one, without causing overloads, memory leaks, or other troubles. How do you do it with C#?

there are three methods that I know of, but which is the best?

  1. method

            ds.Clear(); //disposing of old DataSet
            ds = stats.loadStats(); //loading new
    
  2. method

            ds.Dispose(); //disposing of old DataSet
            ds = stats.loadStats(); //loading new
    
  3. method

            ds = null; //disposing of old DataSet
            ds = stats.loadStats(); //loading new
    
  4. what's your method? (or the best option)

Alex
  • 4,607
  • 9
  • 61
  • 99
  • 1
    Here is a very comprehensive review [(question and answer)][1] of possible alternatives: [1]: http://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable – Ulises Apr 18 '12 at 16:14

1 Answers1

0

All you need is

//ds.Clear(); 
//ds = null; 
ds.Dispose();  // not really needed but out of general principle
ds = stats.loadStats();  // gets a new instance

Leave the rest to the Garbage Collector.

H H
  • 263,252
  • 30
  • 330
  • 514