The following pattern is frequently described in the Code,
Some operations call the server interface to obtain data and perform complex data parsing. These operations cannot block the UI thread and must be executed asynchronously. However, after these operations are executed, the interface elements must be updated.
That is, this pattern frequently appears in the code.
The previous code used thread to solve this problem. The general process is as follows (tasks are encapsulated in the model and these operations are blocked ),
1. First, we must encapsulate these interfaces into the interfaces required by the thread, usually using internal classes (previously we used VS05 without lambda)
2. Call boost: thread
3. PostMessage to a form after execution
4. Modify the form code, and add the message capture and message processing methods.
5. Wait (depending on what additional operations you want and how programmers write)
Anyone who has used this method knows how painful it is and how hard it is to write. Sometimes, it will be lazy, such as updating interface elements in asynchronous mode.
Generally, each step mentioned above is short and small, but the Code must be distributed to different source files in the above method, the reference interaction between objects is added, and the most important problem is that every time you need to execute this mode, it is painful to execute each step above.
What you want is actually very simple: Execute the specified action asynchronously, and remember to update the interface elements after the execution.
Since it is a frequently-occurring mode, encapsulate this mode. You only need to enter
1. What actions do you need to perform?
2. How to update interface elements
Refer to then in C # And we will write the following statements,
Aync ([=] ()-> DWORD_PTR {
Std: string result;
M_impl-> user-> getCrashResult (begin-> first, result );
Return CrashResult: GetResultCount (result );
}). Then ([=] (DWORD_PTR result ){
UpdateCrashResult (unsigned int) begin-> first. fileId (), (int) result );
});
The code in aync is executed in the working thread, and the code in then is executed in the UI thread (now VS version is VS10, with lambda support ).
The implementation idea is as follows,
Aync encapsulates the behaviors in lambda and then as objects and stores them in an array. The interface required by aync is to return a DWORD_PTR type parameter-free function, and the returned value is used as a parameter in then, DWORD_PTR is an int type, which is a pointer. It is very common for passing parameters on Windows platforms. I personally like this because it is flexible in usage.
In aync, lambda is converted to a type without any parameters and return values to adapt to the thread interface, and then placed in the thread pool,
BimWorks: GetAsyncAction (). Append ([&] () {
This-> result _ = func _();
GetBWAsyncInvoke (). Submit (* this );
});
Call GetBWAsyncInvoke (). Submit (* this) after the func _ execution is complete. this method executes the post message to the main thread action. A hidden static form is used as the message receiver.
The message processing function simply finds the corresponding lambda object from the queue and executes the stored then action. Remove the object from the queue after execution.
Complete.
This implementation is only a preliminary method, and some details need to be improved.
This method only applies to a fixed mode, that is, the mode described at the beginning. thread is too low-level and we often need a higher level mode, such as the concept of task, the PPL library in VS10 provides other modes, such as tasks, parallel algorithms, containers, pipelines, and collaboration, which are not very lightweight.