P
P
Popou2021-11-10 04:32:10
ASP.NET
Popou, 2021-11-10 04:32:10

Asp.net How to run a process in the background?

By process, I mean anything (application, windows process) that can run in the background, I have another c # library, and one of its methods takes 2 minutes, I understand that such tasks are clearly not for asp.net, but what can I do .. I don’t need that every user could directly make a request to call this method, I kind of want to put a ban if the method is called and it has not ended, then it cannot be called again.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Bannikov, 2021-11-10
@vabka

Well, there are two options:
Run this work in the HostedService, and put in the concurrent dictionary (or in the database) the information that the user has already called this thing.
Run in a separate process, and store information about the launch in the database.
And communicate through the queue.

M
mletov, 2021-11-10
@mletov

Well, in general, from asp.net you can call, for example, console applications.
https://stackoverflow.com/questions/20212709/calli...
Write the start time, end time to the database and check.
It may be worth looking towards schedulers rather than button actions, Asp.net MVC has Quartz.
https://metanit.com/sharp/mvc5/24.1.php
In .NET (Core) there is
https://codeburst.io/schedule-cron-jobs-using-host...
https://stackoverflow.com/ questions/63795334/in-as...

A
Alexander Yudakov, 2021-11-13
@AlexanderYudakov

static bool IsLongTaskRunning = false;
static object Sync = new object();

static void StartLongTask() {
    lock(Sync) {
        if (IsLongTaskRunning) // Предыдущий вызов еще не завершился
            return;
        IsLongTaskRunning = true;
    }
    Task.Run(async () => {
        Debug.WriteLine("Long task started...");
        try
        {
            await Task.Delay(TimeSpan.FromMinutes(2)); // "Длинный метод" из моей "другой библиотеки"
            Debug.WriteLine("Long task done.");
        } catch(Exception ex) {
            Debug.WriteLine("Long task failed: " + ex);
        }

        lock(Sync) {
            IsLongTaskRunning = false;
        }
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question