首页 新闻 会员 周边 捐助

请教:task的扩展方法,配合async await 多层嵌套后的问题

1
悬赏园豆:5 [已解决问题] 解决于 2022-08-30 08:32

直接上代码,比较烦人,但是还请大神们留步多看几眼,指点一下学习的方向,目前不知道知识点缺失在哪里了。拜谢
当task的扩展方法,与多层 async await 套用,同时 async 后面的返回方式 都是 task的时候,到底是哪个task调用的 task的扩展方法,有点懵,

如下是task 的扩展方法,目的是增加try catch,我在其中增加了 显示 task id和 thread id
public static class TaskUtilities
{

   public static async void FireAndForgetSafeAsync(this Task task, IErrorHandler handler = null)

    {
        try
        {
            Console.WriteLine("进入FireAndForgetSafeAsync thread_id  " + Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("进入FireAndForgetSafeAsync  task_id "+  task.Id);

            await task;

             Console.WriteLine("FireAndForgetSafeAsync 中 await后 thread_id  " + Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("FireAndForgetSafeAsync 中 await后  task_id   " + task.Id);
        }
        catch (Exception ex)
        {
            handler?.HandleError(ex);
        }
    }
}

个人分析1:
当我 单层 创建一个task,引用 扩展方法时,创建的task id号,与进入扩展方法内输出的task id 时一个。请见附件1图
private void button2_Click(object sender, EventArgs e)
{
Console.WriteLine("button2_Click start thread id" + " " + Thread.CurrentThread.ManagedThreadId);
IErrorHandler errorHandler = null; // Get an instance somewhere
Task a = Task.Run(()=>
{
Console.WriteLine("task 内 thread id" + " " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(3000);
});
Thread.Sleep(1000);
Console.WriteLine(" a.FireAndForgetSafeAsync(errorHandler); 前task id "+ a.Id);
a.FireAndForgetSafeAsync(errorHandler);
Thread.Sleep(100);
Console.WriteLine(" a.FireAndForgetSafeAsync(errorHandler); 后task id " + a.Id);
Console.WriteLine("button2_Click end thread id" + " " + Thread.CurrentThread.ManagedThreadId);
}

分析2
当我嵌套使用 async和 await的时候,我发现,出现了3个task id 而且都不一样,没明白问题出在了哪里,为什么会有这么多task id ,到底是那一层返回task,调用的扩展方法呢?

    private void button3_Click(object sender, EventArgs e)
    {
        Console.WriteLine(" button3_Click start  " + Thread.CurrentThread.ManagedThreadId);
        IErrorHandler errorHandler = null; // Get an instance somewhere
         asyncTask().FireAndForgetSafeAsync(errorHandler);
        Thread.Sleep(100);
        Console.WriteLine(" button3_Click end  " + Thread.CurrentThread.ManagedThreadId);
    }
    public async Task asyncTask()
    {
        Task  aa = wasteTime();
        await aa;
        Console.WriteLine(aa.Id);
    }

    public async Task wasteTime()
    {
        Task bb= Task.Run(() =>
        {
            Thread.Sleep(5000);
        });

Thread.Sleep(500);
await bb;
Console.WriteLine(bb.Id);
}

顺便想请教下,thread id和task id间的区别,
分不多了,还想大神们费心指点下,问题方向,再次拜谢,

rossi努力努力的主页 rossi努力努力 | 初学一级 | 园豆:8
提问于:2022-07-15 10:51
< >
分享
最佳答案
0

created() {
this.getKpiRoleButton();
},
methods: {
async getKpiRoleButton() {
try {
const resultObj = await this.signKpiRoleIdSearch();
//resultObj.data.data是id需要传给下面的接口
kpiAPI
.kpiPermissionsButton({
roleId: resultObj.data.data
})
.then(res => {
console.log(res) //处理逻辑
});
} catch (err) {
console.log(err);
}
},
signKpiRoleIdSearch() {
return new Promise((resolve, reject) => {
this.$http
.get("url?userId=" +localStorage.getItem("id"))
.then(res => {
if (res.data.success) {
resolve(res);
}
});
}).catch(error => {
reject(error);
});
}
}

收获园豆:5
wangruidong03 | 初学一级 |园豆:136 | 2022-07-24 09:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册