如何在C#中的控制台应用程序中处理永久WHILE循环中的所有对象

时间:2016-12-24 13:47:17

标签: c# garbage-collection garbage

我需要在他做时间的时候检查数据库中的某些计划任务。

但是当它运行时(没有调用任何其他功能),它每5分钟占用我的硬盘,70MB。这是正常的吗?我该如何解决这个问题?

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 个答案:

答案 0 :(得分:1)

using范围做得很好。 从一次性接口实例化的所有对象将在作用域末尾的using语句中实例化时进行处置。 你不需要做任何其他事情。