c# - Updating Datagrid datacontext eats memory -
i have wpf datagrid , need update every minute. have added dispatchtimer data , update data context of datagrid. problem seems data sticking around. first thinking lists had lists running out updated datacontext , don't see megs of memory being used.
dispatchertimer dispatchertimer = new dispatchertimer(); dispatchertimer.tick += new eventhandler(dispatchertimer_tick); dispatchertimer.interval = new timespan(0, 0, 1);
i have set 1 second testing.
private void dispatchertimer_tick(object sender, eventargs e) { observablecollection<technician> techdata = loaddata(); dg1.datacontext = null; dg1.datacontext = techdata; }
...
private observablecollection<technician> loaddata() { observablecollection<technician> techdata = new observablecollection<technician>(); uri urigreenimage = new uri("pack://application:,,,/images/green.png"); uri uriredimage = new uri("pack://application:,,,/images/red.png"); // connect database. database db = new database(); string query = "select * dbo.statusboard order dbo.statusboard.lastname"; sqldatareader reader = db.query(query); while (reader.read()) { bitmapimage statusimage; switch(reader["status"].tostring()) { case "0": statusimage = new bitmapimage(urigreenimage); break; case "1": statusimage = new bitmapimage(uriredimage); break; default: statusimage = new bitmapimage(uriredimage); break; } techdata.add(new technician { fullname = reader["firstname"].tostring() + " " + reader["lastname"].tostring(), comments = reader["comments"].tostring(), status = statusimage, watchingservicedesk = (reader["watchingservicedesk"].tostring() == "1" ? true : false), phone = reader["phone"].tostring() }); } return techdata; }
Comments
Post a Comment