用户需求:定时抓取ERP系统条码已扫描未审核单据
Workaround:
Thread processing is used because the Timer control consumes system resources.
Because the thread call method does not support the control processing mode, it is implemented by the delegate method.
BOOLbstop; PublicDataTable dt =NewDataTable (); Public scanednotchecked_frm() {InitializeComponent (); }Private void Scanednotchecked_frm_load(Objectsender, EventArgs e) {//define ThreadsThread Logthread =NewThread (NewThreadStart (Doservice));//Set thread to be a background thread, so there will be no programs that are not closed in the processLogthread.isbackground =true; Bstop =true;if(Bstop = =true) {Control.PageControl.CheckForIllegalCrossThreadCalls =false; Logthread.start ();//Up thread} }Private string GetList() {sqlparameter[] parms =Newsqlparameter[0]; Dbcon db =NewDbcon ("ZM"); Gdscanednotchecked.datasource = SqlHelper.ExecuteDataset (db. Stringconnection (), CommandType.StoredProcedure,"dz_scanednotchecked", parms). tables[0];return "Overloaded"; }inti =0;Private void Doservice() { while(true) {bstop =false;//Other line thread call Wow ~i++; Lblmessage.text ="section"+ i.tostring () +"Times"+ returnlist (returnmylist) +"Data"; System.Threading.Thread.Sleep ( +* -*5); } }Private Delegate string returnstrdelegate();//Make a simple drip method ~ Private string returnmylist() {returnGetList (); }//To determine whether to use the Invoke drop ~, not directly return ~ Private string returnlist(Returnstrdelegate mydelegate) {if( This. invokerequired) {return(string) This. Invoke (MyDelegate); }Else{returnMyDelegate (); } }
[C #] Thread and delegate implement timed refresh data function