[C #] Thread and delegate implement timed refresh data function

Source: Internet
Author: User

用户需求:定时抓取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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.