i'm using following code speed retrieving data com object:
const string status = "getting node displacements..."; int count; int totalcount; ienumerable<iloadcase> loadcases; concurrentbag<nodedisplacements> nodedisplacements; // check if results available if (!this.hasresults) return null; // node displacements nodedisplacements = new concurrentbag<nodedisplacements>(); loadcases = this.loadcases.cast<iloadcase>().union(this.loadcombinations.cast<iloadcase>()).where(lc => lc.id >= startloadcase && lc.id <= endloadcase); count = 0; totalcount = this.nodes.count * loadcases.count(); parallel.foreach(this.nodes, (node) => { parallel.foreach(loadcases, (loadcase) => { nodedisplacements.add(this.getnodedisplacements(node, loadcase)); this.onmodelbuildstatusupdate(new modelbuildstatusupdateeventargs(status, interlocked.increment(ref count), totalcount)); }); }); at moment testing via command prompt, following progress through event fired in this.onmodelbuildstatusupdate(...).
because of parallel loops, event fired multiple times. try avoid command prompt being flooded "getting node displacements..." messages tried this:
private static string previoustatusmessage; static void model_modelbuildstatusupdate(staadmodel sender, modelbuildstatusupdateeventargs e) { if (string.isnullorempty(previoustatusmessage) || !previoustatusmessage.equals(e.statusmessage)) { console.writeline(e.statusmessage); previoustatusmessage = e.statusmessage; } console.write("\r{0:0.00%}", e.elementsprocessed / (double)e.totalelementstoprocess); } to display first status message, @ moment displays multiple times (once each outer loop thread?). imaging need lock here, can't figure out what.
i'd appreciate if show me how implement event raising this.
your handler isn't thread safe. try this:
private static string previoustatusmessage; private static object lockobject = new object(); static void model_modelbuildstatusupdate(staadmodel sender, modelbuildstatusupdateeventargs e) { lock (lockobject) { if (string.isnullorempty(previoustatusmessage) || !previoustatusmessage.equals(e.statusmessage)) { console.writeline(e.statusmessage); previoustatusmessage = e.statusmessage; } } console.write("\r{0:0.00%}", e.elementsprocessed / (double)e.totalelementstoprocess); }
Comments
Post a Comment