How to dispose all objects in an eternal WHILE loop in a console appllication in C#

557 views Asked by At

I have to check database for some scheduled task when it is his time do something.

But when it has run (without calling any other function), it occupies my hard drive every 5 min, 70MB. Is this normal? How can I solve this problem?

static void Main(string[] args)
{
    //dhpGroup_kukuEntities _efEntities = new dhpGroup_kukuEntities();
    while (true)
    {
            DateTime ServerNOW= DateTime.Now;

            using (dhpGroup_kukuEntities _efEntities = new dhpGroup_kukuEntities())
            {
                List<Schedual_JSON> ListSchedual = new List<Schedual_JSON>();
                var a =
                    _efEntities.Schedual_JSON.Where(
                        x =>
                            x.SCHJS_DateTimeSchedual.Value.Year == ServerNOW.Year &&
                            x.SCHJS_DateTimeSchedual.Value.Month == ServerNOW.Month &&
                            x.SCHJS_DateTimeSchedual.Value.Day == ServerNOW.Day &&
                            x.SCHJS_DateTimeSchedual.Value.Hour == ServerNOW.Hour &&
                            x.SCHJS_DateTimeSchedual.Value.Minute == ServerNOW.Minute &&
                            x.SCHHS_SendToThread == false).ToList();

                if (a.Count > 0)
                {
                    for (int i = 0; i < a.Count; i++)
                    {
                        Console.WriteLine("One Task Found!");
                    }

                    ThreadPool.QueueUserWorkItem(o => GetSchedualList(a));

                    foreach (var VARIABLE in a)
                    {
                        Console.WriteLine("One Scudule Regarding to User({0}) With {1} Channel(s) and {2} files Run to BasicThread", _efEntities.AspNetUsers.FirstOrDefault(x => x.Id == VARIABLE.SCHJS_UserID).UserName, getChannelCount(VARIABLE), getFileCount(VARIABLE));
                        VARIABLE.SCHHS_SendToThread = true;
                    }

                    _efEntities.SaveChanges();
                }
            }

            System.GC.Collect();
        }
}
1

There are 1 answers

2
Saman Hajizade On

using scope does it well. All objects instantiated from disposable interface,will dispose when instantiated in a using statement at the end of the scope. you don't need to do anything else.