Is the new Thread (() => {// logic}). Start (); acceptable for executing logic asynchronously in web applications page_load

(I use the term "asynchronously" freely in the subject)

I used the code below to store the initial session context information in the database, such as branch details for later tracking. I suggested that others use this concept to speed up some of their processes, but I would not want to offer something potentially dangerous.

I would like to know if this is permissible or are there any problems with the flows that I will cause later on the road during peak traffic hours?

            new Thread(() => {
                //All my logic for saving info to DB
            }) 
            { Name = "My long running page process", IsBackground = true, Priority = ThreadPriority.Normal }.Start();

UPDATE

Thank! After David Basarab's answer, I think this will be my new method:

    Action someLongProcess = () =>
    {
        // You DB Work
    };

    someLongProcess.BeginInvoke((x) => {
        someLongProcess.EndInvoke(x);

        // It is now done you can do something
    }, null);
+3
5

. , . Async, .

Action BeginInvoke, .

Action someLongProcess = () =>
    {
        // You DB Work
    };

AsyncCallback callback = (r) =>
    {
        // It is now done you can do something or pass null to do nothing
    };

someLongProcess.BeginInvoke(callback, null);
+2

, , . Thread , , , .

ThreadPool (Fx 4+).

+1

ThreadPool . " " PostSharp, Thread.Start.

+1

ThreadPool , .

 ThreadPool.SetMaxThreads(4);
 ThreadPool.QueueUserWorkItem(state => { Trace.WriteLine("Hello World."); });

.NET , , . .

+1

Task ThreadPool, Task . , , , - . .

If you have data that needs to be stored in a database, I would recommend using asynchronous pages AsyncOperation(or BackgroundWorker, which uses under it AsyncOperation).

+1
source

Source: https://habr.com/ru/post/1758068/


All Articles