简体   繁体   中英

Multithreaded web service calls in asp.net web forms

I have an aspx page to make calls to a web service, each of which can take up to few minutes and I want to make it work in a multi-threaded way. Basically what I want is when user clicks one of the buttons I want to make a service call to my web service but I also want user to be able to click the other buttons and make other service calls if they want. and if the later calls are completed faster than the first one, results must be shown to user without waiting for the result of the first call. (Yo can think about desktop sql editor applications)

However I can't manage this with my current method. The first call always blocks the following ones, I wonder If I can accomplish this kind of behviour in a web application.

I use a code like the floowing for my service calls:

protected void GetResult_Click(object sender, EventArgs e)
{
   ShowSelectData(sql, connStr, tabId);
}

private void ShowSelectData(string sql, string connStr, string tabId)
{
    SqlServiceClient cli = new SqlServiceClient();
    cli.ShowSelectDataCompleted += cli_ShowSelectDataCompleted;
    cli.ShowSelectDataAsync(sql, connStr, tabId);
 }  
void cli_ShowSelectDataCompleted(object sender, SqlService.ShowSelectDataCompletedEventArgs e)
{
     var res = e.Result;
     //here I show resullts to user
}

I also add Async="true" to my aspx page

Basically what I want is when user clicks one of the buttons I want to make a service call to my web service but I also want user to be able to click the other buttons and make other service calls

What you asked can be accomplished by ajax async call; leave to your client the burden to call the service (by an async call) and manage the response.

When the user click a button make a call to the service; meantime the first call is executing the client can call the second, meantime first and second are executing the client can call the third... the user is not frozen waiting the first call to finish

If you can't call the service directly from a javascript client wrap server side the service calls into rest services and call the rest services from your client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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