简体   繁体   English

使用带参数的异步任务方法注册新的pageasynctask

[英]Register new pageasynctask with async Task method that takes parameters

I have this method in an asp.net webforms project: 我在asp.net webforms项目中有这个方法:

private async Task SomeMethod(int accID){ // }

I want to do this in the page_load, but I'm not sure how to handle the parameters. 我想在page_load中执行此操作,但我不确定如何处理参数。

protected void Page_Load(object sender, EventArgs e)
    {
        RegisterAsyncTask(new PageAsyncTask(SomeMethod(int accID)));

       // etc
}

Try this: 尝试这个:

protected void Page_Load(object sender, EventArgs e)
    {
        RegisterAsyncTask(new PageAsyncTask(() => SomeMethod(accID: 1000)));

       // etc
}

First, I know am too late to answer this, but this may still help others struggling like me. 首先,我知道回答这个问题为时已晚,但这可能仍然有助于其他人像我一样挣扎。

So, you could make all the parameters of the async method optional like below: 因此,您可以将async方法的所有参数设置为可选,如下所示:

private async Task SomeMethod(int accID=0){ // }

and then register the task in the page load event like below: 然后在页面加载事件中注册任务,如下所示:

RegisterAsyncTask(new PageAsyncTask(() => SomeMethod()));

This works fine for me. 这对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM