C # uses interfaces to layer interfaces with background operations

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Threading;usingSystem.Windows.Forms;namespacecallbackdemo{ Public Partial classform1:form,icallback {Device dev=NewDevice ();  PublicForm1 () {InitializeComponent (); Dev.        Start (); }        Private voidButton1_Click (Objectsender, EventArgs e) {Dev. Query ( This); }         Public voidProcess (inti) {MessageBox.Show (i.tostring ()); }    }    Public InterfaceIcallback {voidProcess (inti); }     Public classDevice { Public voidStart () {thread thread=NewThread (proc); Thread. IsBackground=true; Thread.        Start (); }        PrivateIcallback CallBack;  Public voidQuery (Icallback callBack) { This. CallBack =CallBack; Query=1; }        Private intquery =0;  Public voidproc () { while(true) {Thread.Sleep ( +); if(Query = =1)                {                    if(CallBack! =NULL) {callback.process ( -); } Query=0; }            }        }    }}

Change the company, as a newcomer to write Java's Android code. In the processing of the serial port Communication protocol, after parsing the serial protocol content, the data processing part is to be given to the program to complete, the serial thread is only responsible for parsing the data and provide data, how to deal with this data should be given to the interface to do. For example, there is a temperature data in the protocol, how to deal with the data in the protocol layer is not known, how to do it, when the protocol layer processing is put a callback function interface here, that is, the function pointer here. When the protocol layer resolves to the corresponding data, the function of the application layer is invoked according to the function pointer.

The above a small piece of code is very good to complete the above functions, to have a good experience.

C # uses interfaces to layer interfaces with background operations

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.