Task任務取消

2021-10-10 21:23:48 字數 3298 閱讀 5946

**如下

using microsoft.entityframeworkcore;

using system;

using system.collections;

using system.collections.concurrent;

using system.collections.generic;

using system.linq;

using system.threading;

using system.threading.tasks;

namespace efcoredb

#region task and cancellationtokensource

/// /// 任務的鏈式取消

///

public async static void testtasklinkedcancel()

);cancellationtokensource source2 = new cancellationtokensource();

var token2 = source2.token;

token2.register(() => );

cancellationtokensource source3 = cancellationtokensource.createlinkedtokensource(token1, token2);

var token3 = source3.token;

token3.register(() => );

source1.cancelafter(timespan.fromseconds(3));

}/// /// 同步任務和非同步任務的對比

///

public static void testtasksync()

");//建立非同步任務

var taskasync = task.run(() =>

,執行的執行緒:");

int sum = 0;

parallel.for(1, 10000, (i) => );

return sum;

});//建立同步任務

var tasksync = new task(() =>

,執行的執行緒:");

int sum2 = 0;

parallel.for(1, 10000, (i) => );

return sum2;

});tasksync.runsynchronously();

console.writeline($"同步任務:,執行的執行緒的結果:");

console.writeline($"非同步任務:,執行的執行緒的結果:");

}public static void testthreadcancel()

);//不加鎖

parallel.for(1, 10000, i =>

finally

}});

//不加鎖

parallel.for(1, 10000, i =>

});console.writeline($"sun1=");

console.writeline($"sun2=");

console.writeline($"sun3=");

}/// /// 測試取消

///

public static void testtaskthreadcancel()

//}, tokensource.token);

=>

//);

//for (int i = 0; i < 10; i++)

// #endregion

cancellationtokensource tokensource = new cancellationtokensource();

cancellationtoken token = tokensource.token;

try}, token);

token.register(() => );

console.writeline($"task 狀態 :");

task.start();

console.writeline($"task 狀態 :");

tokensource.cancel();

console.writeline($"task 狀態 :");

console.writeline($"task是否取消看下面的 狀態 ");

for (int i = 0; i < 10; i++)

");}

}catch (aggregateexception agg)

");else

console.writeline($"任務取消:");}}

finally

}/// /// 測試取消

///

public static void testtaskcancel()

if (randomnum == 0)

");break;

}vs[j] = randomnum;

}return vs;

}, token));

}try

}return sum / (double)accumulate;

}, token);

console.writeline($"所有任務累加的結果:");

}catch (aggregateexception agg)

");else

console.writeline($"任務取消:");}}

finally

}public static void testtaskcancel1()

if (value == 0)

", iteration);

break;

}values[ctr - 1] = value;

}return values;

}, token));

}try

}return sum / (double)n;

}, token);

console.writeline("the mean is .", ftask.result);

}catch (aggregateexception ae)

",((taskcanceledexception)e).message);

else

console.writeline("exception: " + e.gettype().name);}}

finally

}#endregion

}}

探索Task的取消

我們都知道task.run方法可以傳入乙個cancellationtoken,用於取消。可是有多少人真的去了解過當呼叫cancellationsource.cancel方法時,task是否真的被取消了 我們做個實驗 public static async void foo source.token ...

C Task任務取消

在使用blazor開發工具時,有個需求 掃碼登入頁面需要不斷輪詢介面獲取掃碼狀態,並在對應的狀態下進行對應的操作 重新整理 登入 跳轉等 此時,為了不阻塞前端的響應,需要我們開啟乙個執行緒進行狀態檢測,並在完成後結束執行緒任務 使用取消令牌 cancellationtokensource 相對操作比...

終止任務和取消任務

public class program 當乙個系統 表示處理沒有事件資料的事件的方法。獲取或設定乙個值,該值指示是否對應用程式的cpu和記憶體進行監視為當前程序啟用域,一旦為流程啟用了監視,就不能禁用它。若啟用監察,則為true thread thread new thread thread.st...