Because the job needs to write a simple tool software, the database queries the daily OA does not send the log records of the successful process and batch re-processing Operations.
Started using a single-threaded, background query database when the form of suspended animation, using multithreading is simple to Solve.
Private void Btnquey_click (object sender, EventArgs e) { thisfalse ; New Thread (new ThreadStart (connectdb)); Connectionthread.start (); }
The next problem is tricky, because I call the UI control in the child thread, this time multiple click Query (call sub-thread) will be error, just met when there is no idea, and then inadvertently read an article on the Internet write invoke and BeginInvoke (http/ www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html), The article is very good, and it just happens to solve my problem. This is a problem with the Windows program message mechanism, which affects this mechanism if the child thread invokes the UI control Directly.
Delcheckenvironment delegate is used to check whether the query environment is selected, if you choose to continue to execute the sub-thread, there is a relationship, so use Invoke to marshal the delcheckenvironment to the main thread execution, blocking the Sub-thread execution down Until the marshaled delegate is executed by the main thread.
The following BeginInvoke (delupdategridview,ds) does not need to block the child thread, so that the main thread of the GridView display data set when the child thread also executes, do not need to distinguish the order of Precedence.
Private voidconnectdb () {delcheckenvironment delcheckenvironment=Newdelcheckenvironment (checkenvironment); Invoke (delcheckenvironment); DateTime StartTime= this. datetimepicker1.value; DateTime EndTime= this. datetimepicker2.value; stringSt = Starttime.tostring ("YYYY-MM-DD 00:00:00"); stringSt1 = (startTime + timespan.fromdays (1)). ToString ("YYYY-MM-DD 00:00:00"); stringed = endtime.tostring ("YYYY-MM-DD 00:00:00"); stringed1 = (endTime + timespan.fromdays (1)). ToString ("YYYY-MM-DD 00:00:00"); stringsql ="SELECT distinct senddh,taskid,returnmsg from Ays_process_log where Sendtime >= '"+ St +"' and Sendtime < '"+ St1 +"' and returnmsg not like '% success% ' and returnmsg not like '% received% ' and returnmsg not like '%{\ ' data\ ': {},\ "returncode\": \ "0\"}% ' and returnmsg <> ' and returnmsg not like '%\ ' zsts\ ': 1% ' and returnmsg ' no like '%oa number already exists in SAP system% ' and Tas Kid not in"+"(select distinct Taskid from Process_log where (sendtime >= '"+ St +"' and Sendtime < '"+ ed1 +"') and (returnmsg like '% success% ' or returnmsg like '% received% ' or returnmsg like '%{\ "data\": {},\ "returncode\": \ "0\"}% ' or R Eturnmsg = "or returnmsg like '%\" zsts\ ": 1% ' or returnmsg like '%oa number already exists in SAP system% '))"; DataSet DS=NewDataSet (); Try { using(SqlConnection conn =NewSqlConnection (configurationmanager.connectionstrings[sqlip]. Connectionstring.tostring ())) {using(SqlCommand cmd =NewSqlCommand (sql, CONN)) {cmd.commandtimeout=80000; using(sqldataadapter da =NewSqlDataAdapter (CMD)) {da. Fill (ds,"Process_log"); }}} Delupdategridview Delupdategridview=NewDelupdategridview (updategridview); BeginInvoke (delupdategridview,ds); } Catch(Exception Ex) {messagebox.show (ex). ToString ()); } } Private voidUpdategridview (DataSet Ds) {if(ds. tables[0]. Rows.Count >0) { this. Datagridview1.datasource =ds; this. Datagridview1.datamember ="Ays_process_log"; datagridview1.columns[0]. HeaderText ="OA Tracking number"; datagridview1.columns[0]. Width = -; datagridview1.columns[1]. HeaderText ="TaskId"; datagridview1.columns[1]. Width = -; datagridview1.columns[2]. HeaderText ="Interface Return information"; datagridview1.columns[2]. Width = +; } Else{messagebox.show ("no query to failure ticket"); } this. btnquery.enabled =true; } public voidcheckenvironment () {if(checkenviroment ()) {messagebox.show ("Please select an environment"); this. btnquery.enabled =true; return; } }
WinForm the use of multithreading to handle forms of suspended animation, using invoke BeginInvoke to handle the problem of calling UI controls on a child thread