抛出内存不足错误

时间:2017-02-21 16:01:23

标签: c#

namespace Calendar
{
    public partial class MainCalendar : Form
    {
        private JArray items;
        private List<String> AMList = new List<String>();
        private List<String> PMList = new List<String>();
        private List<String> accessToCalendarFilepath = new List<String>();
        private List<CalendarModel> people;
        private List<List<CalendarModel>> managers = new List<List<CalendarModel>>();
        private List<String> userSelection = new List<String>();
        private bool authorizedAccess = false;
        private String javaScriptFileContainingJSONObject = "";

        public MainCalendar()
        {
            InitializeComponent();           

            var locationInformation = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + "location.json";

            using (StreamReader file = File.OpenText(locationInformation))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JArray o = (JArray)JToken.ReadFrom(reader);
                items = o;
            }

            foreach (var item in items.Children())
            {
                var itemProperties = item.Children<JProperty>();
                // you could do a foreach or a linq here depending on what you need to do exactly with the value
                var myElement = itemProperties.FirstOrDefault(x => x.Name == "name");
                var myElementValue = myElement.Value; ////This is a JValue type

                if(myElementValue.ToString().Contains("AM"))
                {
                    AMList.Add(myElementValue.ToString());
                }

                if (myElementValue.ToString().Contains("PM"))
                {
                    PMList.Add(myElementValue.ToString());
                }
            }

            mondayAM.DataSource = AMList.ToArray();
            tuesdayAM.DataSource = AMList.ToArray();
            wednesdayAM.DataSource = AMList.ToArray();
            thursdayAM.DataSource = AMList.ToArray();
            fridayAM.DataSource = AMList.ToArray();

            mondayPM.DataSource = PMList.ToArray();
            tuesdayPM.DataSource = PMList.ToArray();
            wednesdayPM.DataSource = PMList.ToArray();
            thursdayPM.DataSource = PMList.ToArray();
            fridayPM.DataSource = PMList.ToArray();
            loadAccessControl("accesscontrol.json");
            dateTimePicker1.AlwaysChooseMonday(dateTimePicker1.Value);
            String dateSelected = dateTimePicker1.Value.ToShortDateString();
            findManagerForSelectedDate(dateSelected);
        }

        public void loadAccessControl(String fileName)
        {
            var accessControlInformation = Environment.CurrentDirectory + Path.DirectorySeparatorChar + fileName;
            List<AccessControl> accounts = JsonConvert.DeserializeObject<List<AccessControl>>(File.ReadAllText(accessControlInformation));

            foreach (AccessControl account in accounts)
            {
                Console.WriteLine(account.accountName);

                if (account.accountName.ToLower().Contains(Environment.UserName.ToLower()))
                {
                    foreach (CalendarFile file in account.files)
                    {
                        // Console.WriteLine(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "content" + Path.DirectorySeparatorChar + file.Filename);
                        accessToCalendarFilepath.Add(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "content" + Path.DirectorySeparatorChar + file.Filename);
                    }

                    break;
                }
            }
            contentsOfFile();            
        }

        private void contentsOfFile()
        {
            String line;

            foreach(var file in accessToCalendarFilepath)
            {
                StreamReader contentsOfJSONFile = new StreamReader(file);

                while((line = contentsOfJSONFile.ReadLine()) != null)
                {
                    if(line.Contains("var "))
                    {
                        javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + "[";
                    }
                    else if(line.Contains("];"))
                    {
                        javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + "]";
                    } 
                    else
                    {
                        javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + line;
                    }
                }

                people = JsonConvert.DeserializeObject<List<CalendarModel>>((string)javaScriptFileContainingJSONObject);                
                managers.Add(people);
                javaScriptFileContainingJSONObject = "";
            }
        }

        private void findManagerForSelectedDate(String dateSelected)
        {
            dateSelected = dateTimePicker1.Value.ToShortDateString();

            List<String> managerNames = new List<String>();

            foreach(var item in managers)
            {
                foreach (var subitem in item)
                {
                    CalendarModel c = subitem;
                    Console.WriteLine(c.date);
                    c.name = new CultureInfo("en-US", false).TextInfo.ToTitleCase(c.name);

                    if (userSelection.Count > 0)
                    {
                        foreach (var addedUser in userSelection.ToArray())
                        {
                            if (!addedUser.Contains(c.name))
                            {
                                userSelection.Add(c.name); // CRASHING HERE
 //{"Exception of type 'System.OutOfMemoryException' was thrown."}
                            }
                        }
                    } 
                    else
                    {
                        userSelection.Add(c.name);
                    }
                }
            }

            Console.WriteLine();
        }

我的内存不足。

CalendarModel类:

namespace Calendar
{
    class CalendarModel
    {
        [JsonProperty("name")]
        public string name { get; set; }

        [JsonProperty("date")]
        public string date { get; set; }

        [JsonProperty("title")]
        public string title { get; set; }

        [JsonProperty("mondayAM")]
        public string mondayAM { get; set; }

        [JsonProperty("mondayPM")]
        public string mondayPM { get; set; }

        [JsonProperty("tuesdayAM")]
        public string tuesdayAM { get; set; }

        [JsonProperty("tuesdayPM")]
        public string tuesdayPM { get; set; }

        [JsonProperty("wednesdayAM")]
        public string wednesdayAM { get; set; }

        [JsonProperty("wednesdayPM")]
        public string wednesdayPM { get; set; }

        [JsonProperty("thursdayAM")]
        public string thursdayAM { get; set; }

        [JsonProperty("thursdayPM")]
        public string thursdayPM { get; set; }

        [JsonProperty("fridayAM")]
        public string fridayAM { get; set; }

        [JsonProperty("fridayPM")]
        public string fridayPM { get; set; }

        [JsonProperty("saturdayAM")]
        public string saturdayAM { get; set; }

        [JsonProperty("saturdayPM")]
        public string saturdayPM { get; set; }
    }
}

我一直在崩溃

userSelection.Add(c.name)

1 个答案:

答案 0 :(得分:2)

仔细看看你在做什么

foreach (var addedUser in userSelection.ToArray())
{
    if (!addedUser.Contains(c.name))
    {
        userSelection.Add(c.name);
    }
}

您要在userSelection循环中添加 userSelection 测试在!addedUser.Contains

你甚至不应该这样做,但我认为ToArrray()让它发生

所以你添加了Sally
然后马克
然后再次添加Mark,因为在循环中Mark!= Sally

您没有使用List<String> managerNames = new List<String>();

private void findManagerForSelectedDate(String dateSelected)
{
    dateSelected = dateTimePicker1.Value.ToShortDateString();

你传入dateSelected,然后用dateTimePicker1直接传递,然后你甚至不使用它

你们大多数代码对我来说都没什么意义