Preventing multiple threads for the same routine and the same user?

Asked

Viewed 58 times

2

I’m developing a web app on ASP.NET Core MVC and some routines are called by clicking a button on the web page.

It turns out you can push this button multiple times, so I need some bloqueie/suspenda/... unwanted threads.

I tried to do it with this code (C#), where the variable User is a session variable, corresponding to the user:

[HttpPost]
public async Task<IActionResult> SendOrdForm(
    int id, 
    ProdListViewModel model, 
    IFormCollection form
)
{
    UserModel user = HttpContext.Session.GetJson<UserModel>("User");
    if (user.AwaitingOrderSubmit)
        while (true); // blocks thread or aborts it... What's the best option?

    user.AwaitingOrderSubmit = true;
    HttpContext.Session.SetJson("User", user);
    
    // ... the rest of the long and time-expensive routine ...
    user.AwaitingOrderSubmit = false;
    HttpContext.Session.SetJson("User", user);
    return RedirectToAction("");
}

How can I make this code only executable once thread and user, by code on this or another routine?

  • 2

    I did not understand well, but apparently injecting your service as scoped would already solve, since each request would bring a different instance of service

  • 1

    What do you mean by pressing the button multiple times? You want to block the click of it until the routine is completed?

  • @Leandroangelo : I know how to lock the button in the UI. I’ve already made a modal that blocks the page. However on the server side, a solution that blocks the routine is more secure. To click the button multiple times, any loop is that click the button several times does so. If the UI is not locked is even easier: just click on the button in a short time, while the application is "thinking"...

  • @Lucasmiranda: two clicks on the same button is the same http request? Could you tell me how to inject a service into Startup.Cs that is a simple controller of the main app...?

  • 1

    @Jdias In this case, it is not only with the service scope that you will solve, you will need to add a logical control to define whether or not the routine can be executed, because there is already another processing running.

  • @Leandroangelo What are the keywords to search on the net? Some link with example?

  • 1

    @Jdias what you are looking for is "Competition Control"

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.