Call Async Method in Page_Load
15,166
Solution 1
The question is if you want to make the Page_Load
method async or not. If so:
protected async void Page_Load(object sender, EventArgs e)
{
await SendTweetWithSinglePicture("test", "path");
}
Or if you don't want it to be async
:
protected void Page_Load(object sender, EventArgs e)
{
SendTweetWithSinglePicture("test", "path").Wait();
}
This does require your async
method to return Task
as it always should! (except event handlers)
The problem with this might be that the method doesn't complete before rendering the page. If it has to, you'd better make the method synchronous, or register the task using Page.RegisterAsyncTask
and Page.ExecuteRegisteredAsyncTasks
. Effectively this will freeze the Page_Load
method too.
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask t = new PageAsyncTask(SendTweetWithSinglePicture("test", "path"));
// Register the asynchronous task.
Page.RegisterAsyncTask(t);
// Execute the register asynchronous task.
Page.ExecuteRegisteredAsyncTasks();
}
Solution 2
You should use PageAsyncTask. It has samples in MSDN page.
// Register the asynchronous task.
Page.RegisterAsyncTask(new PageAsyncTask(SendTweetWithSinglePicture(message, image));
// Execute the register asynchronous task.
Page.ExecuteRegisteredAsyncTasks();
as I pointed the sample and explanations on MSDN page is pretty good.
Author by
AnthonyG
Updated on June 04, 2022Comments
-
AnthonyG almost 2 years
static async void SendTweetWithSinglePicture(string message, string image) { var auth = new SingleUserAuthorizer { CredentialStore = new SingleUserInMemoryCredentialStore { ConsumerKey = "", ConsumerSecret = "", AccessToken = "", AccessTokenSecret = "" } }; var context = new TwitterContext(auth); var uploadedMedia = await context.UploadMediaAsync(File.ReadAllBytes(@image)); var mediaIds = new List<ulong> { uploadedMedia.MediaID }; await context.TweetAsync( message, mediaIds ); } protected void Page_Load(object sender, EventArgs e) { SendTweetWithSinglePicture("test", "path"); }
How can I call a
async
method onPage_Load
?-
Ron Beyer over 8 yearsIf you change the method to
static async Task
instead of void, you can call it by usingSendTweetWithSinglePicture("test", "path").Wait()
. Avoidasync void
unless you are using it for events. -
Ron Beyer over 8 yearsBy the way, try to avoid posting your question with your API keys/secrets. Anybody with that information can hijack the API account.
-
sara over 8 years@RonBeyer blocking synchronously in an ASP application is begging for a deadlock. Never ever call Task.Wait() if you can at all avoid it. In this case it's possible to simply make Page_Load async, or register an async task using the built-in types of ASP.
-
-
Ron Beyer over 8 yearsTo use
Wait
the method signature for the SendTweet... needs to be changed toasync Task
doesn't it? -
mason over 8 yearsIt might be more handy if you showed the actual code instead of solely relying on MSDN.
-
Patrick Hofman over 8 yearsWell, yes. Good point. All async methods should (except event handlers).
-
Ron Beyer over 8 yearsWhere is
RegisterAsyncTask
declared? Please try to avoid "try this" type answers. Answers without explaining what changed and why do not help the OP to understand the problem. -
Dilip Oganiya over 8 yearsThis will surely fixed code. Why you upvoting without any reason ??
-
Ron Beyer over 8 yearsI gave a pretty good explanation in my comment, if you'd like to expand on the answer instead of just "try this" then I'd gladly remove the downvote.
-
motime over 7 yearsDownvoting beacuse he wrote "try this" is pure rubbish. Question was how can you call async code from Page_Load and the answer he gave was accurate.
-
Bimal Das over 2 years@hamid , can you tell me why Page.ExecuteRegisteredAsyncTasks() immediately calling the async method? It exit the Page_Load event and execute base master events and then it execute the async task. I want to execute async before calling any other event.
-
Theophilus over 2 yearsThe Using Asynchronous Methods in ASP.NET 4.5 article is also helpful. It was written for Visual Studio 2012 and .NET Framework 4.5 but still seems applicable for V.S. 2019 and Framework 4.8.