Core programming, windows core programming
I. Code protocols
1. Prerequisites
Contract. Requires (bool)
. EndContractBlock (); used to be compatible with the old if judgment code
2. Post Conditions
. Ensures (bool)
. Result <type> () Return Value
. OldValue <type> (x) returns the original value of the Parameter
. ValueAtResult (out x) returns the value of the out parameter.
3. Set check
. Exists () Any item meets the conditions
. ForAll () All items meet the conditions
4. Equality Conditions
. Invariant (bool)
5. Interface binding agreement
[ContractClass (typeof (protocol implementation class)] interface features
[ContractClassFor (typeof (Interface)] Protocol implementation class features
[Pure] Normal Operation Features
Ii. parallel operations 1. Tasks
Task t1 = new Task (Action <object>); create a new Task
Task t3 = new Task (Func <object, result>, data); create a Task that can return results
// TaskCreationOptions specify the Task Type
T1.Start (); start running
Task t2 = t1.ContinueWith (Action <Task>); successor Task. The parameter is the previous Task.
// TaskCreationOptions specifies the startup time
T3.Start ()
T3.Wait () waiting for task completion
T3.Result extraction result
2. Multi-task
Parallel. For (0, 9, Action <int>); from 0 ~ 9 repeat the same method
Parallel. For (, Action <int, ParallelLoopState>); ParallelLoopState controls the iteration process
Parallel. for (, Func <init>, Func <int, ParallelLoopState, init, init2>, Action <init2>); Func <init> initialization task, the second Func is the function executed in each iteration, And the return value of the task is handled by Action. (The number of tasks may be less than the number of iterations, that is, the system will reuse a task, and the start and end tasks are called only when the task is created and the end tasks are completed. Note the information transmitted between functions)
Parallel. ForEach (source <t>, Action <t>) synchronize iterative data sources
Parallel. ForEach (source <t>, Action <t, ParallelLoopState, int>) versions with iterations and Control Parameters
3. Terminate the task
CancellationToken Control Parameters
CancellationTokenSource cts = new CancellationTokenSource (); // create Control Parameters
Cts. Token. Register (Action); // method executed after cancellation of registration
Cts. Cancel (bool); // Cancel the task
Cts. Token // that is, CancellationToken
An OperationCanceledException exception occurs when the task is canceled.
Cts. Token. IsCancellationRequested whether to cancel the task
Cts. Token. ThrowIfCancellationRequested (); An exception is thrown when the instance is canceled.
4. Shared Resource Management
Lock (obj) locks an object or locks the object to define it as a thread-safe object.
The lock sequence of multiple locked objects is inconsistent, which may lead to deadlocks. (My Opinion: either keep the lock sequence consistent, or do not lock Multiple associated objects. method 1 is transformed into a thread-safe object, and each object does not access another thread-safe object before it is unlocked; method 2: Wrap associated objects with a single locked object to make them a whole (because it is difficult to keep the order consistent, or sort by dictionary )).
Lock (this) dangerous statement, because the object may be locked outside the object. The private member of the object should be used as the lock flag.
When reading and writing should be continuous (for example, to avoid saving the old value in a variable that has changed), the entire process should be locked in this transaction-related operation, instead of locking the read and write parts separately (A transaction locks the principle design method, such as providing the read () and readAndWrite () interfaces, rather than the read and write interfaces ).
SpinLock is a structured version of Monitor with better performance.
5. Timer
System. Threading. Timer (Action <object>, obj, TimeSpan, TimeSpan) Call methods at specified intervals
Thread. Sleep (int); Sleep in milliseconds
Iii. Role and authorization IV. XAML 1. Dependency type
Class MyDependencyObject: DependencyObject // specific base class
{Public int Value {get {return (int) GetValue (ValueProperty);} // GetValue, a base class member
Set {SetValue (ValueProperty, value) ;}// call the base class member SetValue
Public static readonly DependecyProperty ValueProperty =
DependecyProperty. Register ("Value", typeof (int), typeof (MyDependencyObject), // Register attributes
New PropertyMetadata (initial value, callback method after value change, callback method before value change ));}
How to Learn "windows core programming"
Don't learn this book from the beginning. This is for experts,
If you have just learned the data structure, you cannot understand this book,
My suggestion is to read the <windows programming> book first. Now it seems that the fifth version has been released.
At the beginning, you don't need to understand everything. You just need to understand the previous message mechanism (Be sure to understand it ),
Then you can write a window according to the code in the book (you don't have to remember the code, but at least you have to input the code, compile it, and run the program properly. Here, this book can be put down first. Of course, it is best to read it later. If you cannot understand it, you have to browse it and know what to say ,.
Then download sun Xin's <VC ++ from entry to mastery> video, and then look back <windows program design>
You can see <windows core programming>
Of course, it will take a long time for me to spend about a year.
Download the complete txt set of c + core Programming Technology
C + core programming technology txt complete novel attachment has been uploaded to Baidu online disk, click to download for free: