1. implement non-UI thread to update the UI threadCode
2. An error in encoding and its exploration
<1>. implement non-UI thread update UI thread
The previous basic practice is to use invoke for implementation. Here we use the task in. Net 4.0 for implementation. The Code is as follows:
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading. tasks; using system. threading; namespace windowsformsapplication1 {public partial class form1: FORM {private readonly taskscheduler m_synccontextscheduler = NULL; Public form1 () {initializecomponent (); base. TEXT = "synchronization context Task Scheduler Demo"; base. visible = true; base. height = 100; base. length = 400; // Attribute m_synccontextscheduler needs to be initialized here M_synccontextscheduler = Taskschedation. fromcurrentsynchronizationcontext (); } Private cancellationtokensource CTS; private taskscheduler context = taskschedcontext. current; // calculate private int32 sum (cancellationtoken token, int32 N) {int32 sum = 0; For (int32 I = 1; I <= N; ++ I) {// for demonstration purpose, the thread is sleeping for a while. sleep (1000); sum + = I;} return sum;} protected override void onmouseclick (mouseeventargs e) {If (this. CTS! = NULL) {// start to cancel the CTS operation. cancel (); this. CTS = NULL;} else {// The operation has not started yet. Start the task this. TEXT = "operation running"; this. CTS = new cancellationtokensource (); var T = new task <int32> () => sum (CTS. token, 10), CTS. token); T. start (); // if the task ends, start the new task and update the UI thread t. continuewith (task => text = "Result:" + T. result, cancellationtoken. none, taskcontinuationoptions. onlyonrantocompletion, this. m_synccontextscheduler); // if it is canceled, T. continuewith (task => text = "Operation cancel", cancellationtoken. none, taskcontinuationoptions. onlyoncanceled, this. m_synccontextscheduler); // if the error t occurs. continuewith (task => This. TEXT = "Operation cancel", cancellationtoken. none, taskcontinuationoptions. onlyonfaulted, this. m_synccontextscheduler);} base. onmouseclick (e );}}}
<2>. Encoding errors and Exploration
The initial code generates the m_synccontextscheduler object through the following statement:
Private readonly taskscheduler m_synccontextscheduler =
Taskschedation. fromcurrentsynchronizationcontext ();
The following error occurs during Debug:
The exception information shows that the current Initialization is still complete, which leads to the initialization sequence of the constructor and attribute in C, the problem is found by debugging or checking the generated il code. in C #, run
Private taskscheduler m_synccontextscheduler =
Taskschedation. fromcurrentsynchronizationcontext ();
At this time, the environment has not been initially completed. More obvious in Il:
. Method public hidebysig specialname rtspecialname instance void. ctor () cel managed {// code size 87 (0x57 ). maxstack 2 il_0000: ldarg.0 il_0001: ldnull il_0002: stfld class [system] system. componentmodel. icontainer windowsformsapplication1.form1: Components il_0007: ldarg.0 Il_0008: call class [mscorlib] system. Threading. Tasks. taskscheduler [mscorlib] system. Threading. Tasks. taskscheduler: fromcurrentsynchronizationcontext () Il_000d: stfld class [mscorlib] system. Threading. Tasks. taskscheduler windowsformsapplication1.form1: m_synccontextscheduler Il_0012: ldarg.0 il_0013: call class [mscorlib] system. threading. tasks. taskscheduler [mscorlib] system. threading. tasks. taskscheduler: get_current () il_0018: stfld class [mscorlib] system. threading. tasks. taskscheduler windowsformsapplication1.form1: context il_001d: ldarg.0
// Initialize and call the form Constructor Il_001e: Call instance void [system. Windows. Forms] system. Windows. Forms. Form:. ctor ()