c# - Why do my database commands only work when I interrupt them with MessageBox.Show() calls? -
with next code, first set of database commands carried out fruition (one table dropped, others have record deleted them) if messagebox.show() phone call @ origin of droptablesanddeletefromtables() commented out.
if uncomment it, user has dismiss after each set of database manipulations (obviously not want in version used customers), - tables dropped, , references them deleted, desired. why interrupting process in way (computus interruptus?) create difference between success , failure, , how can have pie , eat it, (get database commands succeed without bothering user n messagebox.show() dialogs dismiss?
private void droptablesanddeletefromtables(string recordtype, string filename) { messagebox.show(string.format("in droptablesanddeletefromtables(), recordtype {0}, filename {1}", recordtype, filename)); //todo: remove seek { workfiles wrkfile = new workfiles(); int tableok = 0; dataset workfiles; tableok = wrkfile.isvalidworktable(); if (tableok > 0) //table has @ to the lowest degree 1 record { workfiles = wrkfile.getallrecords(); //go thru dataset , find filename clean after foreach (datarow row in workfiles.tables[0].rows) { string tmptype = row["filetype"].tostring(); if (tmptype.endswith("0") || tmptype.endswith("1")) { tmptype = tmptype.substring(0, 3); } string tmpstr = row["name"].tostring(); int intsite = (int) row["siteno"]; string tmpname = tmptype + "_" + intsite.tostring() + "_" + tmpstr; if (tmptype != recordtype) continue; if (tmpname != filename) continue; //drop worktables table site-specific db [ such hhsdb003.sdf ] string droptable = "drop table " + tmptype + tmpstr; string delworktablesimple = string.format("delete worktables filetype = '{0}' , name = '{1}'", tmptype, tmpstr); string delworktable0 = "delete worktables filetype = '" + tmptype + "0' , name = '" + tmpstr + "'"; string delworktable1 = "delete worktables filetype = '" + tmptype + "1' , name = '" + tmpstr + "'"; // site-specific database first // 0) drop table contents have been sent sendcommandtodb(droptable, intsite, true); pausethatrefreshes(); // 1) delete record site-specific [ hhsdb[sitenum].sdf worktables, such hhsdb003.sdf ] sendcommandtodb(delworktablesimple, intsite, true); pausethatrefreshes(); // bypassing "0" , "1" tables did nil - still drops 1 table , deletes // 2) same 1, table named [dsd,inv}0_bla sendcommandtodb(delworktable0, intsite, true); pausethatrefreshes(); // 3) same 2, table named [dsd,inv}1_bla instead of [dsd,inv}0_bla sendcommandtodb(delworktable1, intsite, true); pausethatrefreshes(); // 4 calls site-specific above; three-four calls non-site-specific below // 4) delete record non-site-specific [ hhsdb[sitenum].sdf worktables, such hhsdb003.sdf ] sendcommandtodb(delworktablesimple, intsite, false); pausethatrefreshes(); // 5) same 1, table named [dsd,inv}0_bla sendcommandtodb(delworktable0, intsite, false); pausethatrefreshes(); // 6) same 2, table named [dsd,inv}1_bla instead of [dsd,inv}0_bla sendcommandtodb(delworktable1, intsite, false); pausethatrefreshes(); // 7) conditionally delete record (if dsd record, dsdheader, in base of operations (non-site-specific) database if (tmptype == "dsd") { string dml = string.format("delete {0}header name = '{1}'", tmptype, tmpstr); sendcommandtodb(dml, intsite, false); } populatetransactionlistboxwithworktables(); return; } // foreach (datarow row in workfiles.tables[0].rows) } // if ( tableok > 0) //table exist //} // lock tfs#4054 } // seek grab (exception ex) { sscs.exceptionhandler(ex, "frmcentral.droptablesanddeletefromtables"); } } // droptablesanddeletefromtables private void pausethatrefreshes() { int j = 0; while (j < 100000) { j++; } } private void sendcommandtodb(string sql, int sitenum, bool sitespecificdb) { seek { if (sitespecificdb) { if (dbconn.inbasedatabase()) { dbconn = dbconnection.getinstance(sitenum.tostring()); } } else { if (!(dbconn.inbasedatabase())) { dbconn = dbconnection.getinstance(); } } dbconn.dbcommand(sql, true); } grab (sqlceexception ee) { . . . } }
what workaround allow process come air without forcing user play role in charade?
updateit seems matter of how much time elapses between each set of database manipulations. when changed this:
while (i < 100000)
... in pausethatrefreshes() this:
while (i < 10000000)
(with messagebox.show() commented out) worked! still makes me nervous. there more "scientific" (elegant?) way accomplish this?
your code illustration both complicated, , incomplete. can't sure what's wrong.
but symptom classic indication a) running code in question on main gui thread, , b) code @ point winds blocked waiting main gui thread else (i.e. deadlock).
the right way prepare perform operations on different thread main gui thread. introduce new problems accessing gui elements operation, you'll have address using gui api's "invoke" mechanism.
c# sql-server-ce compact-framework windows-ce messagebox
No comments:
Post a Comment