c 中返回Task與T的大概用法

2021-09-26 08:16:58 字數 1122 閱讀 6184

在某專案中,因需要對乙個通用控制項做**重構,安排一些事件前檢查點與事件後檢查點,方便在具體業務中,對些通用控制項做繼承後,加入特定的業務控制邏輯。

在重構**的的過程中,把主要處理邏輯分成三部分:precheck(),check(),aftercheck()。又為了方便在各個方法體中使用非同步方法獲取資料,把三個方法的型別,都定義成task。這種時就碰到了小問題:如果方法體中,不需要使用非同步方法時,如果定義return的值。

經過測試,有了如下發現:如果方法定義為 protected virtual task precheck() 時,需要 return task.factory.startnew(()=> ); 來對應;但如果定義成 protected virtual async task check()時,可以直接return true; 來對應;在繼承類中也一樣的。

示例**如下:

1、main方法:

static async system.threading.tasks.task main(string args)

2、mybaseclass類

public class mybaseclass

else

if (!await check())

else

if (!await aftercheck())

else

console.writeline(「well done」);

}

protected virtual taskprecheck());}

protected virtual async taskcheck()

protected virtual taskaftercheck()

);}

}

3、extendclass類

class extendclass:mybaseclass

protected override async taskcheck()

else

}protected override taskaftercheck()

private async taskanotherprecheck()

}

東莞**建設www.zg886.cn

c 中返回Task與T的大概用法

在某專案中,因需要對乙個通用控制項做 重構,安排一些事件前檢查點與事件後檢查點,方便在具體業務中,對些通用控制項做繼承後,加入特定的業務控制邏輯。在重構 的的過程中,把主要處理邏輯分成三部分 precheck check aftercheck 又為了方便在各個方法體中使用非同步方法獲取資料,把三個方...

verilog中的task用法

定義乙個任務。task task demo 任務定義結構開頭,命名為 task demo input 7 0 x,y 輸入埠說明 output 7 0 tmp 輸出埠說明 if x y 給出任務定義的描述語句 tmp x else tmp y endtask 上述 定義了乙個名為 task demo...

verilog中的task用法

定義乙個任務。task task demo 任務定義結構開頭,命名為 task demo input 7 0 x,y 輸入埠說明 output 7 0 tmp 輸出埠說明 if x y 給出任務定義的描述語句 tmp x else tmp y endtask 上述 定義了乙個名為 task demo...