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