c# - DataTable rapid population -
good day!
i have multi-thread application, purpose, in general, process fast populating message queue. messages added queue 1 thread , thread process them.
in processing thread messages must shown in grid. grid's datasource datatable. processing thread works datatable (on each message processing; nid, sid, bid - part of 1 message):
lockers[5].waitone(); continue = true; (int = 0; <= mdatasource.tables["stations"].rows.count - 1; i++) { datarow current = mdatasource.tables["stations"].rows[i]; if ((ushort)current["sid"] == sid && (ushort)current["nid"] == nid && (ushort)current["bid"] == bid) { continue = false; break; } } if (continue &&) { synccontext.post(x => { mdatasource.tables["stations"].beginloaddata(); mdatasource.tables["stations"].rows.add( (fbn + 1) * 10 + 1 + dn, devicelist[fbn].main.receivers[dn].channel, devicelist[fbn].main.receivers[dn].pilot, sid, nid, bid, devicelist[fbn].receivers[dn].mcc, latitude, longitude); mdatasource.tables["stations"].endloaddata(); }, null); } lockers[5].releasemutex();
lockers[5] - mutex object, mdatasource - dataset object. syncontext - synchronization context of main application form. above message queue quite fast populating, code fires 500 times in 1 second in runtime. , in message queue may repeats, mustn't shown them. problem on few first iterations of message processing thread mdatasource.tables["stations"].rows.count = 0, because of continue variable = true , have repeats in table. in next iterations see in logs rows added in table, count may still 2 (but in logs there 17 rows added). why be?
i tried change mutex object on lock(){} operator. not helped me. tried change datatable on bindinglist. not helped me. can lower count of repeats in table creating global variable private readonly int[] lastaddedstation = new int[3]; , add such expression in if block (if (continue){}): lastaddedstation[0] != sid || lastaddedstation[1] != nid || lastaddedstation[2] != bid . helped me lower repeats in table, but, in opinion, "smells". there clear ways situation?
what doing wrong? best practices on high load work datatables?
p.s. i'm sorry english, i'm not native speaker.
mainly, answered here - what difference between synchronizationcontext.send , synchronizationcontext.post? .
post method works in asynchronous mode - "drop off , continue". seems on next iteration code in post method hasn't been completed yet.
i choose add separate counter count added rows , check datatable.count .
Comments
Post a Comment