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?
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
– Lucas Miranda
What do you mean by pressing the button multiple times? You want to block the click of it until the routine is completed?
– Leandro Angelo
@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"...
– JDias
@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...?
– JDias
@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.
– Leandro Angelo
@Leandroangelo What are the keywords to search on the net? Some link with example?
– JDias
@Jdias what you are looking for is "Competition Control"
– Leandro Angelo