One person mentioned today that I have been blinded. The current requirement is,CodeI'm dizzy. I just want to write it down. There's no big theory.
Private delegate void delegateformstartsend (); // defines a delegate to encapsulate the method as a function pointer.
Public void controluploads ()
{
Delegateformstartsend d = new delegateformstartsend (controlupload); // wrap controlupload to the delegate to obtain the function pointer
This. begininvoke (D, null); // This pointer is passed as a parameter in the legend (actually this is a winform ).ProgramThe multi‑thread updates the page fragment.) then, you can call the controlupload method according to the policy in this method. (This is probably the case in begininvoke method, then you can execute the controlupload method.
If (delegateformstartsend! = NULL)
{
Delegateformstartsend ();
}
)
}
Public void controlupload ()
{
This. pbarstate. value = wholebagcountcurrent + 1;
}
What is a callback function?
In short, a callback function is a function called through a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to which it points, we will say this is a callback function.
Why should I use a callback function?
Because the caller and the called can be separated. The caller does not care who is called. All they need to know is that there is a called function with a specific prototype and certain restrictions (for example, the returned value is int.
If you want to know what the callback function has in practice, first assume that there is such a situation, we need to write a library, which provides some sortingAlgorithmSuch as Bubble sorting, quick sorting, shell sorting, and shake sorting. However, to make the database more generic, you do not want to embed the sorting logic in the function, let the user implement the corresponding logic; or, what should I do if I want the database to be used for multiple data types (INT, float, string? Function pointers can be used for callback.
Callback can be used in the notification mechanism. For example, sometimes you need to set a timer in the program. at a certain time, the program will receive the corresponding notification, but the implementer of the notification mechanism knows nothing about our program. At this time, we need a function pointer of a specific prototype, which is used for callback to notify us that the program event has occurred. In fact, the settimer () API uses a callback function to notify the timer. In case a callback function is not provided, it sends a message to the Message Queue of the program.
Another API function using the callback mechanism is enumwindow (), which enumerates all the top-level windows on the screen, calls a function provided by a program for each window, and transmits the processing program of the window. If the caller returns a value, iteration continues. Otherwise, exit. Enumwindow () does not care about where the caller is or what the handler passed by the caller is. It only cares about the returned value because it will continue to execute or exit based on the returned value.
In any case, the callback function continues from the C language. Therefore, in C ++, you should only create an interface with C code or deal with an existing callback interface, to use the callback function. In addition to the above cases, the virtual method or function operator should be used in C ++, rather than the callback function.